中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

acg的wordpress主題深圳百度推廣優(yōu)化

acg的wordpress主題,深圳百度推廣優(yōu)化,學(xué)什么技術(shù)月入上萬,電商平臺(tái)哪個(gè)最好散列表(Hash Table)是一種高效的數(shù)據(jù)結(jié)構(gòu),廣泛用于實(shí)現(xiàn)快速的鍵值對(duì)存儲(chǔ)。 基本概念 散列表使用哈希函數(shù)將鍵映射到數(shù)組的索引。其主要優(yōu)點(diǎn)在于平均情況下提供常數(shù)時(shí)間復(fù)雜度的查找、插入和刪除操作。 哈希函數(shù): 將鍵映射到一個(gè)固定大小的…

散列表(Hash Table)是一種高效的數(shù)據(jù)結(jié)構(gòu),廣泛用于實(shí)現(xiàn)快速的鍵值對(duì)存儲(chǔ)。

基本概念

散列表使用哈希函數(shù)將鍵映射到數(shù)組的索引。其主要優(yōu)點(diǎn)在于平均情況下提供常數(shù)時(shí)間復(fù)雜度的查找、插入和刪除操作。

  • 哈希函數(shù): 將鍵映射到一個(gè)固定大小的數(shù)組索引。一個(gè)好的哈希函數(shù)應(yīng)該具備:
    • 散列均勻性:不同的鍵應(yīng)該盡量映射到不同的索引。
    • 計(jì)算簡單:哈希值的計(jì)算應(yīng)該高效。

沖突處理

由于多個(gè)鍵可能映射到同一個(gè)索引,必須采取措施處理沖突。常見的沖突解決方法包括:

  • 鏈地址法: 在每個(gè)數(shù)組索引處使用鏈表存儲(chǔ)所有映射到該索引的鍵值對(duì)。
  • 開放尋址法: 在數(shù)組中查找下一個(gè)可用位置,例如線性探測、二次探測和雙重散列等方法。

性能分析

時(shí)間復(fù)雜度:

  • 查找:O(1)(平均情況),O(n)(最壞情況,發(fā)生沖突時(shí))
  • 插入:O(1)(平均情況),O(n)(最壞情況)
  • 刪除:O(1)(平均情況),O(n)(最壞情況)

空間復(fù)雜度: O(n),即存儲(chǔ)元素的數(shù)量。

負(fù)載因子(Load Factor): 定義為元素?cái)?shù)量與表大小的比率。一般在負(fù)載因子超過一定閾值(如0.7)時(shí)進(jìn)行擴(kuò)容,以保持性能。

代碼實(shí)現(xiàn)

鏈地址法

#include <iostream>
#include <vector>
#include <list>
#include <utility> // for std::pair
#include <functional> // for std::hashtemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0) {}void Insert(const Key& key, const Value& value) {size_t index = Hash_(key) % table.size();for (auto& pair : table[index]) {if (pair.first == key) {pair.second = value; // 更新值return;}}table[index].emplace_back(key, value); // 插入新鍵值對(duì)current_size++;if (current_size > table.size() * load_factor) {Resize_();}}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();for (const auto& pair : table[index]) {if (pair.first == key) {value = pair.second;return true;}}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();auto& cell = table[index];for (auto it = cell.begin(); it != cell.end(); ++it) {if (it->first == key) {cell.erase(it); // 刪除current_size--;return true;}}return false; // 未找到}private:std::vector<std::list<std::pair<Key, Value>>> table; // 哈希表的數(shù)組size_t current_size; // 當(dāng)前存儲(chǔ)的元素?cái)?shù)量const float load_factor = 0.7; // 負(fù)載因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用標(biāo)準(zhǔn)哈希函數(shù)}void Resize_() {std::vector<std::list<std::pair<Key, Value>>> old_table = table;table.resize(old_table.size() * 2); // 擴(kuò)容current_size = 0;for (const auto& cell : old_table) {for (const auto& pair : cell) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 輸出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 輸出: apple not found}return 0;
}

代碼解析

  1. 數(shù)據(jù)結(jié)構(gòu):
    • 使用 std::vector 存儲(chǔ)鏈表,鏈表用于處理沖突。
    • 每個(gè)鏈表中的元素是 std::pair<Key, Value>,用于存儲(chǔ)鍵值對(duì)。
  2. 插入操作:
    • 計(jì)算哈希值并確定索引。
    • 檢查索引處是否存在相同的鍵,如果存在則更新值,否則插入新鍵值對(duì)。
    • 如果當(dāng)前元素個(gè)數(shù)超過負(fù)載因子,則調(diào)用 Resize 擴(kuò)容。
  3. 查找操作:
    • 計(jì)算索引,遍歷鏈表查找對(duì)應(yīng)的鍵。
  4. 刪除操作:
    • 計(jì)算索引并在鏈表中查找鍵,找到后刪除。
  5. 擴(kuò)容:
    • 創(chuàng)建新的、更大的表,重新插入舊表中的元素以保證均勻分布。

開放尋址法

#include <iostream>
#include <vector>
#include <utility> // for std::pair
#include <stdexcept> // for std::out_of_rangetemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0), load_factor(0.7) {}void Insert(const Key& key, const Value& value) {if (current_size >= table.size() * load_factor) {Resize_();}size_t index = Hash_(key) % table.size();while (table[index].first != Key() && table[index].first != key) {index = (index + 1) % table.size(); // 線性探測}table[index] = { key, value };current_size++;}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {value = table[index].second;return true;}index = (index + 1) % table.size(); // 線性探測}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {table[index] = { Key(), Value() }; // 標(biāo)記為刪除current_size--;return true;}index = (index + 1) % table.size(); // 線性探測}return false; // 未找到}private:std::vector<std::pair<Key, Value>> table; // 散列表的數(shù)組size_t current_size; // 當(dāng)前存儲(chǔ)的元素?cái)?shù)量const float load_factor; // 負(fù)載因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用標(biāo)準(zhǔn)哈希函數(shù)}void Resize_() {std::vector<std::pair<Key, Value>> old_table = table;table.resize(old_table.size() * 2, { Key(), Value() }); // 擴(kuò)容current_size = 0;for (const auto& pair : old_table) {if (pair.first != Key()) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 輸出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 輸出: apple not found}return 0;
}

代碼解析

  1. 數(shù)據(jù)結(jié)構(gòu):
    • 使用 std::vector<std::pair<Key, Value>> 存儲(chǔ)鍵值對(duì)。未使用的槽位初始化為 Key()Value(),用于標(biāo)記空槽。
  2. 插入操作:
    • 計(jì)算哈希值并確定初始索引。
    • 如果發(fā)生沖突,使用線性探測法查找下一個(gè)可用的索引。
    • 如果當(dāng)前元素?cái)?shù)量超過負(fù)載因子,則調(diào)用 Resize 方法進(jìn)行擴(kuò)容。
  3. 查找操作:
    • 計(jì)算索引并線性探測,直到找到對(duì)應(yīng)的鍵或到達(dá)空槽。
  4. 刪除操作:
    • 在查找過程中,如果找到目標(biāo)鍵,則標(biāo)記該位置為已刪除。
  5. 擴(kuò)容:
    • 創(chuàng)建一個(gè)更大的數(shù)組并重新插入舊表中的元素,以保持均勻分布。

總結(jié)

散列表是一種高效且靈活的數(shù)據(jù)結(jié)構(gòu),適合用于需要快速查找和存儲(chǔ)的場景。通過合理設(shè)計(jì)哈希函數(shù)和沖突處理策略,可以實(shí)現(xiàn)良好的性能。

http://www.risenshineclean.com/news/54050.html

相關(guān)文章:

  • 主流網(wǎng)站模板網(wǎng)址查詢域名
  • java 企業(yè)網(wǎng)站開發(fā)搜索引擎優(yōu)化簡稱seo
  • 濟(jì)南企業(yè)建站系統(tǒng)企業(yè)員工培訓(xùn)總結(jié)
  • 電子商務(wù)網(wǎng)站實(shí)例建站模板平臺(tái)
  • 網(wǎng)站建設(shè)推廣方案網(wǎng)絡(luò)項(xiàng)目推廣平臺(tái)
  • 用wex5可以做網(wǎng)站嗎中國培訓(xùn)網(wǎng)的證書含金量
  • 怎樣做社交網(wǎng)站互聯(lián)網(wǎng)廣告推廣好做嗎
  • 可以做軟件的網(wǎng)站有哪些功能成都百度業(yè)務(wù)員電話
  • 網(wǎng)站控制面板網(wǎng)站查詢
  • 哪家公司做網(wǎng)站比較好成都百度seo公司
  • 昆明企業(yè)建網(wǎng)站多少錢萬網(wǎng)域名注冊(cè)官網(wǎng)
  • 怎樣建設(shè)與維護(hù)自己的平臺(tái)網(wǎng)站成都競價(jià)托管多少錢
  • 酒店預(yù)定網(wǎng)站建設(shè)方案北京疫情太嚴(yán)重了
  • 網(wǎng)站建設(shè)專項(xiàng)檢查上海seo怎么優(yōu)化
  • 怎么用自己的電腦做服務(wù)器發(fā)布網(wǎng)站濟(jì)南做seo排名
  • 百度網(wǎng)站提交入口百度怎么發(fā)外鏈
  • 高仿服裝網(wǎng)站建設(shè)東莞關(guān)鍵詞seo優(yōu)化
  • 微信公眾平臺(tái)小程序是什么寧波seo網(wǎng)站服務(wù)
  • 北京做網(wǎng)站站長工具麻豆
  • 做室內(nèi)裝修的網(wǎng)站seo排名優(yōu)化培訓(xùn)價(jià)格
  • 酷炫網(wǎng)站設(shè)計(jì)風(fēng)格北京做網(wǎng)站推廣
  • 網(wǎng)站建設(shè)這個(gè)工作怎么樣天津建站網(wǎng)
  • 公司網(wǎng)站建設(shè)方案詳細(xì)方案軟文推廣產(chǎn)品
  • 北京網(wǎng)站開發(fā)網(wǎng)站開發(fā)公司seo網(wǎng)站優(yōu)化培訓(xùn)找哪些
  • 通遼做家教的網(wǎng)站seo營銷優(yōu)化
  • 貼吧廣告投放seo前線
  • 武漢網(wǎng)站建設(shè) 網(wǎng)站制作市場營銷的策劃方案
  • 做外包的網(wǎng)站有哪些優(yōu)化大師官方下載
  • 高端定制網(wǎng)站開發(fā)網(wǎng)站模板設(shè)計(jì)關(guān)鍵詞搜索愛站網(wǎng)
  • seo 服務(wù)如何優(yōu)化網(wǎng)站