{"id":89751,"date":"2026-08-03T16:33:17","date_gmt":"2026-08-03T08:33:17","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/89751.html"},"modified":"2026-08-03T16:33:17","modified_gmt":"2026-08-03T08:33:17","slug":"%e5%8d%81%e4%ba%8c-%e6%95%a3%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/89751.html","title":{"rendered":"\u5341\u4e8c.\u6563\u5217\u8868"},"content":{"rendered":"<h3>\u4e00\u3001\u57fa\u7840\u6982\u5ff5\u7c7b<\/h3>\n<p>\u6563\u5217\u8868&#xff08;Hash Table&#xff0c;\u4e5f\u53eb\u54c8\u5e0c\u8868&#xff09;&#xff0c;\u6838\u5fc3\u601d\u60f3\u662f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4&#xff1a;\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u628a key \u6620\u5c04\u6210\u6570\u7ec4\u4e0b\u6807&#xff0c;\u76f4\u63a5\u6309\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20&#xff0c;\u628a\u67e5\u627e\u590d\u6742\u5ea6\u4ece O (n) \u964d\u5230\u5e73\u5747 O (1)\u3002<\/p>\n<p>\u6838\u5fc3\u672f\u8bed<\/p>\n<ul>\n<li>\u54c8\u5e0c\u51fd\u6570&#xff1a;\u628a\u4efb\u610f\u957f\u5ea6\u7684 key&#xff0c;\u8f6c\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u4e0b\u6807\u7684\u51fd\u6570&#xff0c;\u8bb0\u4e3a hash(key) &#061; index\u3002<\/li>\n<li>\u54c8\u5e0c\u51b2\u7a81&#xff1a;\u4e24\u4e2a\u4e0d\u540c\u7684 key&#xff0c;\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u5f97\u5230\u76f8\u540c\u7684\u4e0b\u6807&#xff0c;\u5c31\u662f\u51b2\u7a81&#xff0c;\u5fc5\u987b\u6709\u89e3\u51b3\u673a\u5236\u3002<\/li>\n<li>\u8d1f\u8f7d\u56e0\u5b50&#xff08;Load Factor&#xff09;&#xff1a;\u8d1f\u8f7d\u56e0\u5b50 &#061; \u5143\u7d20\u4e2a\u6570 \/ \u6570\u7ec4\u957f\u5ea6&#xff0c;\u8861\u91cf\u6563\u5217\u8868\u7684\u62e5\u6324\u7a0b\u5ea6&#xff1b;\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5927&#xff0c;\u51b2\u7a81\u6982\u7387\u8d8a\u9ad8&#xff0c;\u6027\u80fd\u8d8a\u5dee\u3002<\/li>\n<li>\u6269\u5bb9&#xff08;Rehash&#xff09;&#xff1a;\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6&#xff0c;\u7533\u8bf7\u66f4\u5927\u7684\u65b0\u6570\u7ec4&#xff0c;\u628a\u6240\u6709\u5143\u7d20\u91cd\u65b0\u54c8\u5e0c\u642c\u8fc7\u53bb&#xff0c;\u4fdd\u8bc1\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u63d0\u95ee&#xff1a;\u597d\u7684\u54c8\u5e0c\u51fd\u6570\u5e94\u8be5\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u56db\u4e2a\u6838\u5fc3\u8981\u6c42&#xff1a;<\/p>\n<li>\u8ba1\u7b97\u5feb&#xff1a;\u54c8\u5e0c\u51fd\u6570\u672c\u8eab\u4e0d\u80fd\u592a\u590d\u6742&#xff0c;\u5426\u5219\u8ba1\u7b97\u5f00\u9500\u62b5\u6d88\u4e86\u67e5\u627e\u6536\u76ca\u3002<\/li>\n<li>\u5206\u5e03\u5747\u5300&#xff1a;\u5c3d\u53ef\u80fd\u628a key \u5747\u5300\u5206\u5e03\u5230\u6574\u4e2a\u6570\u7ec4\u4e0a&#xff0c;\u51cf\u5c11\u51b2\u7a81\u6982\u7387\u3002<\/li>\n<li>\u786e\u5b9a\u6027&#xff1a;\u540c\u4e00\u4e2a key&#xff0c;\u6bcf\u6b21\u8ba1\u7b97\u7684\u7ed3\u679c\u5fc5\u987b\u5b8c\u5168\u4e00\u81f4&#xff0c;\u4e0d\u80fd\u53d8\u3002<\/li>\n<li>\u96ea\u5d29\u6548\u5e94&#xff1a;key \u5fae\u5c0f\u7684\u53d8\u5316&#xff0c;\u4f1a\u5bfc\u81f4\u54c8\u5e0c\u503c\u5de8\u5927\u5dee\u5f02&#xff0c;\u907f\u514d\u76f8\u4f3c\u7684 key \u96c6\u4e2d\u5728\u4e00\u5757\u4ea7\u751f\u51b2\u7a81\u3002<\/li>\n<p>\u5de5\u4e1a\u754c\u5e38\u7528\u7684\u54c8\u5e0c\u51fd\u6570\u6709 MurmurHash\u3001CityHash\u3001XXHash&#xff0c;\u90fd\u662f\u9ad8\u6027\u80fd\u4e14\u5206\u5e03\u5747\u5300\u7684\u4ee3\u8868\u3002<\/p>\n<p>\u63d0\u95ee&#xff1a;\u5e38\u89c1\u7684\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u65b9\u6cd5\u6709\u54ea\u4e9b&#xff1f;\u5404\u81ea\u6709\u4ec0\u4e48\u4f18\u52a3&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u4e3b\u6d41\u6709\u4e24\u5927\u7c7b&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6cd5\u539f\u7406\u4f18\u52bf\u52a3\u52bf\u4ee3\u8868\u5b9e\u73b0<\/tr>\n<tbody>\n<tr>\n<td>\u94fe\u5730\u5740\u6cd5<\/td>\n<td>\u6bcf\u4e2a\u6570\u7ec4\u4f4d\u7f6e\u6302\u4e00\u6761\u94fe\u8868&#xff0c;\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u653e\u5728\u540c\u4e00\u6761\u94fe\u4e0a<\/td>\n<td>\u7b80\u5355\u76f4\u89c2&#xff0c;\u589e\u5220\u65b9\u4fbf&#xff0c;\u6269\u5bb9\u7b80\u5355&#xff0c;\u51b2\u7a81\u5904\u7406\u6210\u672c\u7a33\u5b9a<\/td>\n<td>\u94fe\u8868\u6709\u6307\u9488\u5f00\u9500&#xff0c;\u5185\u5b58\u4e0d\u8fde\u7eed&#xff0c;\u7f13\u5b58\u547d\u4e2d\u7387\u4f4e<\/td>\n<td>Java HashMap\u3001C&#043;&#043; unordered_map<\/td>\n<\/tr>\n<tr>\n<td>\u5f00\u653e\u5bfb\u5740\u6cd5<\/td>\n<td>\u51b2\u7a81\u4e86\u5c31\u6309\u4e00\u5b9a\u89c4\u5219\u627e\u4e0b\u4e00\u4e2a\u7a7a\u4f4d&#xff0c;\u76f4\u5230\u627e\u5230\u4e3a\u6b62<\/td>\n<td>\u5168\u90e8\u5b58\u5728\u6570\u7ec4\u91cc&#xff0c;\u5185\u5b58\u8fde\u7eed&#xff0c;\u7f13\u5b58\u53cb\u597d&#xff0c;\u65e0\u6307\u9488\u5f00\u9500<\/td>\n<td>\u5220\u9664\u9ebb\u70e6&#xff08;\u8981\u6807\u8bb0\u5220\u9664&#xff09;&#xff0c;\u51b2\u7a81\u7d2f\u52a0\u6548\u5e94\u660e\u663e&#xff0c;\u8d1f\u8f7d\u56e0\u5b50\u4e0d\u80fd\u592a\u9ad8<\/td>\n<td>Python \u5b57\u5178\u3001Go map\u3001ThreadLocalMap<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f00\u653e\u5bfb\u5740\u6cd5\u53c8\u5206\u4e09\u79cd\u63a2\u6d4b\u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\u7ebf\u6027\u63a2\u6d4b&#xff1a;\u51b2\u7a81\u4e86\u5c31\u5f80\u540e\u627e\u4e0b\u4e00\u4e2a\u7a7a\u4f4d&#xff0c;\u7b80\u5355\u4f46\u5bb9\u6613\u4ea7\u751f \u201c\u5806\u79ef\u201d&#xff0c;\u51b2\u7a81\u8d8a\u6765\u8d8a\u591a<\/li>\n<li>\u4e8c\u6b21\u63a2\u6d4b&#xff1a;\u6b65\u957f\u662f\u5e73\u65b9\u7ea7\u589e\u957f&#xff0c;\u7f13\u89e3\u5806\u79ef\u95ee\u9898<\/li>\n<li>\u53cc\u91cd\u54c8\u5e0c&#xff1a;\u7528\u7b2c\u4e8c\u4e2a\u54c8\u5e0c\u51fd\u6570\u7b97\u6b65\u957f&#xff0c;\u5206\u5e03\u6700\u5747\u5300&#xff0c;\u8ba1\u7b97\u7565\u6162<\/li>\n<\/ul>\n<p>\u63d0\u95ee&#xff1a;\u4e3a\u4ec0\u4e48\u6563\u5217\u8868\u8981\u6269\u5bb9&#xff1f;\u4ec0\u4e48\u65f6\u5019\u6269\u5bb9&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u6563\u5217\u8868\u7684\u6027\u80fd\u548c\u8d1f\u8f7d\u56e0\u5b50\u5f3a\u76f8\u5173&#xff1a;\u5143\u7d20\u8d8a\u591a\u3001\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5927&#xff0c;\u51b2\u7a81\u6982\u7387\u8d8a\u9ad8&#xff0c;\u67e5\u627e\u6548\u7387\u5c31\u4f1a\u4ece O (1) \u9000\u5316\u3002\u6269\u5bb9\u5c31\u662f\u4e3a\u4e86\u628a\u8d1f\u8f7d\u56e0\u5b50\u964d\u4e0b\u6765&#xff0c;\u7ef4\u6301 O (1) \u7684\u5e73\u5747\u6027\u80fd\u3002<\/p>\n<p>\u6269\u5bb9\u65f6\u673a\u7531\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\u51b3\u5b9a&#xff1a;<\/p>\n<ul>\n<li>\u94fe\u5730\u5740\u6cd5&#xff1a;\u9608\u503c\u4e00\u822c\u5728 0.75 \u5de6\u53f3&#xff08;Java HashMap \u5c31\u662f 0.75&#xff09;&#xff0c;\u5e73\u8861\u7a7a\u95f4\u548c\u51b2\u7a81\u7387\u3002<\/li>\n<li>\u5f00\u653e\u5bfb\u5740\u6cd5&#xff1a;\u9608\u503c\u66f4\u4f4e&#xff0c;\u4e00\u822c\u5728 0.6~0.7&#xff0c;\u56e0\u4e3a\u5f00\u653e\u5bfb\u5740\u5bf9\u8d1f\u8f7d\u56e0\u5b50\u66f4\u654f\u611f&#xff0c;\u592a\u9ad8\u4e86\u51b2\u7a81\u4f1a\u6025\u5267\u589e\u52a0\u3002<\/li>\n<\/ul>\n<p>\u6269\u5bb9\u8fc7\u7a0b&#xff1a;\u7533\u8bf7\u4e00\u4e2a\u539f\u5927\u5c0f 2 \u500d\u7684\u65b0\u6570\u7ec4&#xff0c;\u904d\u5386\u65e7\u6570\u7ec4\u6240\u6709\u5143\u7d20&#xff0c;\u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c\u503c\u653e\u5230\u65b0\u6570\u7ec4\u91cc&#xff0c;\u6700\u540e\u66ff\u6362\u6389\u65e7\u6570\u7ec4\u3002<\/p>\n<h3>\u4e8c\u3001\u6838\u5fc3\u64cd\u4f5c\u4ee3\u7801<\/h3>\n<h4>1. \u94fe\u5730\u5740\u6cd5\u5b9e\u73b0\u6563\u5217\u8868<\/h4>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;list&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class HashTable {<br \/>\nprivate:<br \/>\n    vector&lt;list&lt;pair&lt;string, int&gt;&gt;&gt; table;  \/\/ \u6570\u7ec4\u6bcf\u4e2a\u4f4d\u7f6e\u662f\u4e00\u6761\u94fe\u8868<br \/>\n    int size;\/\/ \u5143\u7d20\u4e2a\u6570<br \/>\n    int capacity;   \/\/ \u6570\u7ec4\u5bb9\u91cf<br \/>\n    double loadFactorThreshold;  \/\/ \u8d1f\u8f7d\u56e0\u5b50\u9608\u503c<\/p>\n<p>    \/\/ \u54c8\u5e0c\u51fd\u6570&#xff1a;\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4\u4e0b\u6807<br \/>\n    int hash(const string&amp; key) {<br \/>\n        unsigned int hashVal &#061; 0;<br \/>\n        for (char c : key) {<br \/>\n            hashVal &#061; hashVal * 31 &#043; c;\/\/ \u7ecf\u5178BKDR\u54c8\u5e0c\u7b97\u6cd5<br \/>\n        }<br \/>\n        return hashVal % capacity;<br \/>\n    }<\/p>\n<p>    \/\/ \u6269\u5bb9&#xff1a;\u5bb9\u91cf\u7ffb\u500d&#xff0c;\u91cd\u65b0\u54c8\u5e0c\u6240\u6709\u5143\u7d20<br \/>\n    void rehash() {<br \/>\n        int oldCapacity &#061; capacity;<br \/>\n        capacity *&#061; 2;<br \/>\n        vector&lt;list&lt;pair&lt;string, int&gt;&gt;&gt; newTable(capacity);<\/p>\n<p>        \/\/ \u904d\u5386\u65e7\u8868\u6240\u6709\u5143\u7d20&#xff0c;\u91cd\u65b0\u54c8\u5e0c\u5230\u65b0\u8868<br \/>\n        for (auto&amp; bucket : table) {<br \/>\n            for (auto&amp; kv : bucket) {<br \/>\n                int idx &#061; hash(kv.first);<br \/>\n                newTable[idx].push_back(kv);<br \/>\n            }<br \/>\n        }<br \/>\n        table &#061; move(newTable);<br \/>\n    }<br \/>\npublic:<br \/>\n    HashTable(int cap &#061; 16) : capacity(cap), size(0), loadFactorThreshold(0.75) {<br \/>\n        table.resize(capactiy);<br \/>\n    }<\/p>\n<p>    \/\/ \u63d2\u5165\/\u66f4\u65b0<br \/>\n    void put(const string&amp; key, int value) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        \/\/ \u5148\u67e5\u662f\u5426\u5df2\u5b58\u5728&#xff0c;\u5b58\u5728\u5219\u66f4\u65b0<br \/>\n        for (auto&amp; kv : table[idx]) {<br \/>\n            if (kv.first &#061;&#061; key) {<br \/>\n                kv.second &#061; value;<br \/>\n                return;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u4e0d\u5b58\u5728\u5219\u63d2\u5165\u94fe\u8868\u5934\u90e8<br \/>\n        table[idx].push_front({key, value});<br \/>\n        size &#043;&#043; ;<br \/>\n        \/\/ \u8d85\u8fc7\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c&#xff0c;\u6269\u5bb9<br \/>\n        if ((double)size \/ capacity &gt; loadFactorThreshold) {<br \/>\n            rehash();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u67e5\u627e&#xff0c;\u627e\u5230\u8fd4\u56de\u503c&#xff0c;\u627e\u4e0d\u5230\u8fd4\u56de-1<br \/>\n    int get(const string&amp; key) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        for (auto&amp; kv : table[idx]) {<br \/>\n            if (kv.first &#061;&#061; key) {<br \/>\n                return kv.second;<br \/>\n            }<br \/>\n        }<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u5220\u9664<br \/>\n    bool remove(const string&amp; key) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        for (auto it &#061; table[idx].begin(); it !&#061; table[idx].end(); &#043;&#043;it) {<br \/>\n            if (it-&gt;first &#061;&#061; key) {<br \/>\n                table[idx].erase(it);<br \/>\n                size &#8211;;<br \/>\n                return true;<br \/>\n            }<br \/>\n        }<br \/>\n        return false;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u63d0\u9192&#xff1a;\u54c8\u5e0c\u51fd\u6570\u9009 BKDR \u7b97\u6cd5&#xff08;\u4e58 31 \u52a0\u5b57\u7b26&#xff09;&#xff0c;\u7b80\u5355\u597d\u5199\u4e14\u5206\u5e03\u5747\u5300&#xff0c;\u4e34\u573a\u6f14\u793a\u9996\u9009\u3002<\/p>\n<h4>2. \u5f00\u653e\u5bfb\u5740\u6cd5\u5b9e\u73b0\u6563\u5217\u8868<\/h4>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>class HashTableOpen {<br \/>\nprivate:<br \/>\n    vector&lt;pair&lt;string, int&gt;&gt; table;<br \/>\n    vector&lt;bool&gt; occupied; \/\/ \u6807\u8bb0\u8be5\u4f4d\u7f6e\u662f\u5426\u6709\u5143\u7d20<br \/>\n    vector&lt;bool&gt; deleted;  \/\/ \u6807\u8bb0\u8be5\u4f4d\u7f6e\u662f\u5426\u662f\u5220\u9664\u7684&#xff08;\u5893\u7891\u6807\u8bb0&#xff09;<br \/>\n    int size;<br \/>\n    int capacity;<\/p>\n<p>    int hash(const string&amp; key) {<br \/>\n        unsigned int hashVal &#061; 0;<br \/>\n        for (char c : key) {<br \/>\n            hashVal &#061; hashVal * 31 &#043; c;<br \/>\n        }<br \/>\n        return hashVal % capacity;<br \/>\n    }<\/p>\n<p>public:<br \/>\n    HashTableOpen(int cap &#061; 16) : capacity(cap), size(0) {<br \/>\n        table.resize(capacity);<br \/>\n        occupied.resize(capacity, false);<br \/>\n        deleted.resize(capacity, false);<br \/>\n    }<\/p>\n<p>    void put(const string&amp; key, int value) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        \/\/ \u7ebf\u6027\u63a2\u6d4b\u627e\u7a7a\u4f4d<br \/>\n        while (occupied[idx] &amp;&amp; !deleted[idx]) {<br \/>\n            if (table[idx].first &#061;&#061; key) {<br \/>\n                table[idx].second &#061; value; \/\/ \u5df2\u5b58\u5728&#xff0c;\u66f4\u65b0<br \/>\n                return;<br \/>\n            }<br \/>\n            idx &#061; (idx &#043; 1) % capacity;<br \/>\n        }<br \/>\n        table[idx] &#061; {key, value};<br \/>\n        occupied[idx] &#061; true;<br \/>\n        deleted[idx] &#061; false;<br \/>\n        size&#043;&#043;;<br \/>\n    }<\/p>\n<p>    int get(const string&amp; key) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        while (occupied[idx]) {<br \/>\n            if (!deleted[idx] &amp;&amp; table[idx].first &#061;&#061; key) {<br \/>\n                return table[idx].second;<br \/>\n            }<br \/>\n            idx &#061; (idx &#043; 1) % capacity;<br \/>\n        }<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u61d2\u5220\u9664&#xff1a;\u53ea\u6807\u8bb0&#xff0c;\u4e0d\u771f\u6b63\u5220\u9664&#xff0c;\u4fdd\u8bc1\u63a2\u6d4b\u94fe\u4e0d\u65ad<br \/>\n    bool remove(const string&amp; key) {<br \/>\n        int idx &#061; hash(key);<br \/>\n        while (occupied[idx]) {<br \/>\n            if (!deleted[idx] &amp;&amp; table[idx].first &#061;&#061; key) {<br \/>\n                deleted[idx] &#061; true;<br \/>\n                size&#8211;;<br \/>\n                return true;<br \/>\n            }<br \/>\n            idx &#061; (idx &#043; 1) % capacity;<br \/>\n        }<br \/>\n        return false;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u63d0\u9192&#xff1a;\u5f00\u653e\u5bfb\u5740\u6cd5\u5220\u9664\u5fc5\u987b\u7528\u5893\u7891\u6807\u8bb0&#xff08;\u61d2\u5220\u9664&#xff09;&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u6e05\u7a7a&#xff0c;\u5426\u5219\u63a2\u6d4b\u94fe\u4f1a\u65ad&#xff0c;\u540e\u9762\u7684\u5143\u7d20\u5c31\u627e\u4e0d\u5230\u4e86<\/p>\n<h3>\u4e09\u3001\u8fdb\u9636\u7406\u89e3\u7c7b<\/h3>\n<p>\u63d0\u95ee&#xff1a;\u4e3a\u4ec0\u4e48 Java HashMap \u94fe\u8868\u957f\u5ea6\u8d85\u8fc7 8 \u8981\u8f6c\u7ea2\u9ed1\u6811&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u5de5\u7a0b\u6743\u8861&#xff1a;<\/p>\n<ul>\n<li>\u6b63\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u5206\u5e03\u5747\u5300&#xff0c;\u94fe\u8868\u957f\u5ea6\u5f88\u77ed&#xff0c;\u67e5\u627e\u5f88\u5feb&#xff0c;\u94fe\u8868\u5b9e\u73b0\u7b80\u5355\u5f00\u9500\u5c0f\u3002<\/li>\n<li>\u6781\u7aef\u60c5\u51b5\u4e0b&#xff08;\u54c8\u5e0c\u51fd\u6570\u4e0d\u597d\u3001\u6076\u610f\u6784\u9020\u51b2\u7a81 key&#xff09;&#xff0c;\u94fe\u8868\u4f1a\u53d8\u5f97\u5f88\u957f&#xff0c;\u67e5\u627e\u9000\u5316\u6210 O (n)&#xff0c;\u6027\u80fd\u6025\u5267\u4e0b\u964d\u3002<\/li>\n<li>\u5f53\u94fe\u8868\u957f\u5ea6\u5230 8 \u65f6&#xff0c;\u8f6c\u6210\u7ea2\u9ed1\u6811&#xff0c;\u67e5\u627e\u53d8\u6210 O (logn)&#xff0c;\u4fdd\u8bc1\u6781\u7aef\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\u5e95\u7ebf\u3002<\/li>\n<li>\u4e3a\u4ec0\u4e48\u662f 8&#xff1f;\u6839\u636e\u6cca\u677e\u5206\u5e03&#xff0c;\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u7684\u6982\u7387\u4e0d\u5230\u5343\u4e07\u5206\u4e4b\u4e00&#xff0c;\u6b63\u5e38\u60c5\u51b5\u51e0\u4e4e\u4e0d\u4f1a\u89e6\u53d1&#xff0c;\u53ea\u6709\u5f02\u5e38\u60c5\u51b5\u624d\u4f1a\u8f6c\u6811&#xff0c;\u517c\u987e\u4e86\u6b63\u5e38\u60c5\u51b5\u7684\u6027\u80fd\u548c\u6781\u7aef\u60c5\u51b5\u7684\u5b89\u5168\u6027\u3002<\/li>\n<li>\u5143\u7d20\u5c11\u7684\u65f6\u5019\u518d\u8f6c\u56de\u94fe\u8868&#xff0c;\u56e0\u4e3a\u5c0f\u6570\u636e\u91cf\u4e0b\u94fe\u8868\u904d\u5386\u6bd4\u7ea2\u9ed1\u6811\u66f4\u5feb&#xff0c;\u4e14\u5185\u5b58\u5f00\u9500\u66f4\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u63d0\u95ee&#xff1a;\u6563\u5217\u8868\u6269\u5bb9\u4e3a\u4ec0\u4e48\u4e00\u822c\u662f 2 \u500d\u6269\u5bb9&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u4e24\u4e2a\u6838\u5fc3\u539f\u56e0&#xff1a;<\/p>\n<li>\u4f4d\u8fd0\u7b97\u4f18\u5316\u53d6\u6a21&#xff1a;\u5f53\u5bb9\u91cf\u662f 2 \u7684 n \u6b21\u5e42\u65f6&#xff0c;hash % capacity \u53ef\u4ee5\u7b49\u4ef7\u66ff\u6362\u6210 hash &amp; (capacity &#8211; 1)&#xff0c;\u4f4d\u8fd0\u7b97\u6bd4\u53d6\u6a21\u8fd0\u7b97\u5feb\u5f88\u591a&#xff0c;\u6027\u80fd\u66f4\u597d\u3002<\/li>\n<li>\u6269\u5bb9\u8fc1\u79fb\u9ad8\u6548&#xff1a;2 \u500d\u6269\u5bb9\u540e&#xff0c;\u5143\u7d20\u5728\u65b0\u8868\u7684\u4f4d\u7f6e\u53ea\u6709\u4e24\u79cd\u53ef\u80fd&#xff1a;\u8981\u4e48\u5728\u539f\u4e0b\u6807&#xff0c;\u8981\u4e48\u5728\u300c\u539f\u4e0b\u6807 &#043; \u65e7\u5bb9\u91cf\u300d\u7684\u4f4d\u7f6e&#xff0c;\u4e0d\u7528\u91cd\u65b0\u8ba1\u7b97\u5b8c\u6574\u54c8\u5e0c&#xff0c;\u76f4\u63a5\u770b\u54c8\u5e0c\u503c\u7684\u67d0\u4e00\u4f4d\u5c31\u80fd\u786e\u5b9a\u65b0\u4f4d\u7f6e&#xff0c;\u8fc1\u79fb\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n<p>\u63d0\u95ee&#xff1a;\u4ec0\u4e48\u662f\u4e00\u81f4\u6027\u54c8\u5e0c&#xff1f;\u89e3\u51b3\u4e86\u4ec0\u4e48\u95ee\u9898&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;<\/p>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c\u662f\u5206\u5e03\u5f0f\u573a\u666f\u4e0b\u7684\u54c8\u5e0c\u7b97\u6cd5&#xff0c;\u89e3\u51b3\u7684\u662f\u666e\u901a\u54c8\u5e0c\u6269\u5bb9 \/ \u7f29\u5bb9\u65f6&#xff0c;\u5927\u91cf key \u5931\u6548\u8fc1\u79fb\u7684\u95ee\u9898\u3002<\/p>\n<p>\u666e\u901a\u54c8\u5e0c&#xff1a;key % n&#xff0c;\u5982\u679c\u8282\u70b9\u6570\u4ece n \u53d8\u6210 n&#043;1&#xff0c;\u51e0\u4e4e\u6240\u6709 key \u7684\u6620\u5c04\u4f4d\u7f6e\u90fd\u4f1a\u53d8&#xff0c;\u5168\u91cf\u8fc1\u79fb\u6210\u672c\u6781\u9ad8\u3002<\/p>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c&#xff1a;\u628a\u54c8\u5e0c\u7a7a\u95f4\u7ec4\u7ec7\u6210\u4e00\u4e2a 0~2^32-1 \u7684\u73af\u5f62&#xff0c;\u8282\u70b9\u548c key \u90fd\u6620\u5c04\u5230\u73af\u4e0a&#xff0c;key \u987a\u65f6\u9488\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u5c31\u662f\u5b83\u5f52\u5c5e\u7684\u8282\u70b9\u3002\u589e\u51cf\u8282\u70b9\u65f6&#xff0c;\u53ea\u4f1a\u5f71\u54cd\u76f8\u90bb\u7684\u4e00\u5c0f\u6bb5 key&#xff0c;\u7edd\u5927\u90e8\u5206 key \u7684\u6620\u5c04\u4e0d\u53d8&#xff0c;\u8fc1\u79fb\u91cf\u6781\u5c0f\u3002<\/p>\n<p>\u4e00\u822c\u8fd8\u4f1a\u52a0\u865a\u62df\u8282\u70b9&#xff0c;\u89e3\u51b3\u8282\u70b9\u5c11\u65f6\u5206\u5e03\u4e0d\u5747\u3001\u6570\u636e\u503e\u659c\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5178\u578b\u5e94\u7528&#xff1a;\u5206\u5e03\u5f0f\u7f13\u5b58&#xff08;Redis \u96c6\u7fa4&#xff09;\u3001\u8d1f\u8f7d\u5747\u8861\u3001\u5206\u5e03\u5f0f\u5b58\u50a8\u3002<\/p>\n<p>\u95ee\u9898&#xff1a;\u666e\u901a\u54c8\u5e0c\u4e3a\u4ec0\u4e48\u4e0d\u884c&#xff1f;<\/p>\n<p>\u5047\u8bbe\u6709 3 \u53f0\u7f13\u5b58\u670d\u52a1\u5668&#xff08;Node0\u3001Node1\u3001Node2&#xff09;&#xff0c;\u8981\u628a key \u5206\u5e03\u5230\u8fd9 3 \u53f0\u673a\u5668\u4e0a\u3002<\/p>\n<p>\u666e\u901a\u53d6\u6a21\u54c8\u5e0c&#xff1a;<\/p>\n<p>\u670d\u52a1\u5668\u7f16\u53f7 &#061; hash(key) % N   &#xff08;N &#061; \u670d\u52a1\u5668\u6570\u91cf&#xff09;<\/p>\n<p>\u4e00\u65e6\u8282\u70b9\u6570\u53d8\u4e86&#xff1a;<\/p>\n<table>\n<tr>\u573a\u666f\u53d8\u5316\u53d7\u5f71\u54cd\u7684 key \u6bd4\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>3 \u53f0 \u2192 4 \u53f0&#xff08;\u6269\u5bb9&#xff09;<\/td>\n<td>N \u4ece 3 \u53d8 4<\/td>\n<td>\u7ea6 75% \u7684 key \u6620\u5c04\u5173\u7cfb\u5931\u6548<\/td>\n<\/tr>\n<tr>\n<td>4 \u53f0 \u2192 3 \u53f0&#xff08;\u7f29\u5bb9&#xff09;<\/td>\n<td>N \u4ece 4 \u53d8 3<\/td>\n<td>\u7ea6 75% \u7684 key \u6620\u5c04\u5173\u7cfb\u5931\u6548<\/td>\n<\/tr>\n<tr>\n<td>N \u53f0 \u2192 N&#043;1 \u53f0<\/td>\n<td>\u4e00\u822c\u60c5\u51b5<\/td>\n<td>\u7ea6 N\/(N&#043;1) \u7684 key \u5168\u90e8\u5931\u6548<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u51e0\u4e4e\u6240\u6709\u6570\u636e\u90fd\u8981\u91cd\u65b0\u8fc1\u79fb&#xff0c;\u7f13\u5b58\u96ea\u5d29\u7ea7\u522b\u7684\u51b2\u51fb\u3002<\/p>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c\u539f\u7406<\/p>\n<p>1\u3001\u628a\u6574\u4e2a\u54c8\u5e0c\u503c\u7a7a\u95f4&#xff08;\u6bd4\u5982 0 ~ 2\u00b3\u00b2-1&#xff09;\u60f3\u8c61\u6210\u4e00\u4e2a\u9996\u5c3e\u76f8\u63a5\u7684\u5706\u73af<\/p>\n<p>2\u3001\u628a\u670d\u52a1\u5668\u8282\u70b9\u6620\u5c04\u5230\u73af\u4e0a&#xff0c;\u5bf9\u6bcf\u4e2a\u670d\u52a1\u5668\u8282\u70b9&#xff08;\u7528 IP\u3001ID \u7b49\u552f\u4e00\u6807\u8bc6&#xff09;\u505a\u54c8\u5e0c&#xff0c;\u628a\u7ed3\u679c\u843d\u5728\u73af\u4e0a\u7684\u67d0\u4e2a\u4f4d\u7f6e<\/p>\n<p>3\u3001\u6570\u636e\u53bb\u627e\u5bf9\u5e94\u7684\u670d\u52a1\u5668&#xff1a;\u5bf9\u6570\u636e\u7684 key \u4e5f\u505a\u54c8\u5e0c&#xff0c;\u843d\u5728\u73af\u4e0a\u67d0\u4e2a\u4f4d\u7f6e&#xff0c;\u7136\u540e\u987a\u65f6\u9488\u627e&#xff0c;\u9047\u5230\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u5c31\u662f\u76ee\u6807\u670d\u52a1\u5668&#xff0c;\u7406\u60f3\u60c5\u51b5\u4e0b&#xff0c;\u589e\u5220\u4e00\u4e2a\u8282\u70b9&#xff0c;\u53ea\u9700\u8981\u8fc1\u79fb 1\/N \u7684\u6570\u636e\u3002<\/p>\n<p>\u865a\u62df\u8282\u70b9&#xff0c;\u89e3\u51b3\u6570\u636e\u503e\u659c<\/p>\n<p>\u8282\u70b9\u592a\u5c11\u65f6\u5206\u5e03\u4e0d\u5747&#xff0c;\u6bd4\u5982\u4e09\u4e2a\u8282\u70b9\u6324\u5728\u4e00\u8d77&#xff0c;\u5927\u90e8\u5206 key \u90fd\u843d\u5230 NodeA \u4e0a\u4e86 &#xff0c;\u8fd9\u5c31\u662f\u6570\u636e\u503e\u659c\u3002<\/p>\n<p>\u4e0d\u7ed9\u6bcf\u4e2a\u771f\u5b9e\u8282\u70b9\u53ea\u6620\u5c04\u4e00\u4e2a\u70b9&#xff0c;\u800c\u662f\u6620\u5c04\u51e0\u5341\u4e2a\u751a\u81f3\u51e0\u767e\u4e2a\u865a\u62df\u70b9&#xff0c;\u589e\u5220\u65f6\u5f71\u54cd\u5206\u6563\u5728\u73af\u7684\u5404\u5904&#xff0c;\u4e0d\u4f1a\u96c6\u4e2d\u51b2\u51fb\u67d0\u4e00\u4e2a\u8282\u70b9<\/p>\n<p>\u4ee3\u7801\u7b80\u5355\u5b9e\u73b0&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;map&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4e00\u81f4\u6027\u54c8\u5e0c\u73af&#xff08;\u5e26\u865a\u62df\u8282\u70b9&#xff09;<br \/>\n *<br \/>\n * \u539f\u7406&#xff1a;<br \/>\n * &#8211; \u7528 std::map \u7ef4\u62a4&#034;\u54c8\u5e0c\u503c \u2192 \u771f\u5b9e\u8282\u70b9\u540d&#034;\u7684\u6709\u5e8f\u6620\u5c04&#xff08;\u6a21\u62df\u54c8\u5e0c\u73af&#xff09;<br \/>\n * &#8211; \u6bcf\u4e2a\u771f\u5b9e\u8282\u70b9\u5bf9\u5e94\u591a\u4e2a\u865a\u62df\u8282\u70b9&#xff0c;\u5747\u5300\u5206\u5e03\u5728\u73af\u4e0a<br \/>\n * &#8211; \u67e5\u627e key \u65f6&#xff0c;\u627e\u7b2c\u4e00\u4e2a &gt;&#061; key\u54c8\u5e0c\u503c \u7684\u865a\u62df\u8282\u70b9<br \/>\n * &#8211; \u5982\u679c\u627e\u4e0d\u5230&#xff08;key \u5728\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u4e4b\u540e&#xff09;&#xff0c;\u53d6\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff08;\u73af\u7684\u7279\u6027&#xff09;<br \/>\n *\/<br \/>\nclass ConsistentHash {<br \/>\npublic:<br \/>\n    \/**<br \/>\n     * &#064;brief \u6784\u9020\u51fd\u6570<br \/>\n     * &#064;param virtual_node_count \u6bcf\u4e2a\u771f\u5b9e\u8282\u70b9\u5bf9\u5e94\u7684\u865a\u62df\u8282\u70b9\u6570\u91cf<br \/>\n     *\/<br \/>\n    explicit ConsistentHash(int virtual_node_count &#061; 150)<br \/>\n        : virtual_node_count_(virtual_node_count) {}<\/p>\n<p>    \/**<br \/>\n     * &#064;brief \u6dfb\u52a0\u4e00\u4e2a\u771f\u5b9e\u8282\u70b9<br \/>\n     * &#064;param node  \u771f\u5b9e\u8282\u70b9\u6807\u8bc6&#xff08;\u5982 IP\u3001\u4e3b\u673a\u540d&#xff09;<br \/>\n     *<br \/>\n     * \u4e3a\u8be5\u8282\u70b9\u751f\u6210 virtual_node_count_ \u4e2a\u865a\u62df\u8282\u70b9&#xff0c;\u5168\u90e8\u52a0\u5165\u54c8\u5e0c\u73af<br \/>\n     *\/<br \/>\n    void addNode(const std::string&amp; node) {<br \/>\n        for (int i &#061; 0; i &lt; virtual_node_count_; &#043;&#043;i) {<br \/>\n            \/\/ \u865a\u62df\u8282\u70b9 key &#061; &#034;\u771f\u5b9e\u8282\u70b9#\u5e8f\u53f7&#034;&#xff0c;\u518d\u54c8\u5e0c<br \/>\n            std::string virtual_key &#061; node &#043; &#034;#&#034; &#043; std::to_string(i);<br \/>\n            size_t hash &#061; std::hash&lt;std::string&gt;{}(virtual_key);<br \/>\n            hash_ring_[hash] &#061; node;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * &#064;brief \u79fb\u9664\u4e00\u4e2a\u771f\u5b9e\u8282\u70b9<br \/>\n     * &#064;param node  \u8981\u79fb\u9664\u7684\u771f\u5b9e\u8282\u70b9\u6807\u8bc6<br \/>\n     *<br \/>\n     * \u628a\u8be5\u8282\u70b9\u5bf9\u5e94\u7684\u6240\u6709\u865a\u62df\u8282\u70b9\u4ece\u54c8\u5e0c\u73af\u4e0a\u5220\u6389<br \/>\n     *\/<br \/>\n    void removeNode(const std::string&amp; node) {<br \/>\n        for (int i &#061; 0; i &lt; virtual_node_count_; &#043;&#043;i) {<br \/>\n            std::string virtual_key &#061; node &#043; &#034;#&#034; &#043; std::to_string(i);<br \/>\n            size_t hash &#061; std::hash&lt;std::string&gt;{}(virtual_key);<br \/>\n            hash_ring_.erase(hash);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * &#064;brief \u6839\u636e key \u627e\u5230\u5e94\u8be5\u53bb\u7684\u771f\u5b9e\u8282\u70b9<br \/>\n     * &#064;param key  \u6570\u636e\u7684 key<br \/>\n     * &#064;return \u76ee\u6807\u771f\u5b9e\u8282\u70b9\u540d&#xff1b;\u73af\u4e3a\u7a7a\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<br \/>\n     *<br \/>\n     * \u6838\u5fc3\u67e5\u627e\u903b\u8f91&#xff1a;<br \/>\n     * 1. \u5bf9 key \u505a\u54c8\u5e0c<br \/>\n     * 2. \u5728\u6709\u5e8f map \u4e2d\u627e\u7b2c\u4e00\u4e2a &gt;&#061; key\u54c8\u5e0c\u503c \u7684\u8282\u70b9&#xff08;lower_bound&#xff09;<br \/>\n     * 3. \u627e\u5230\u4e86\u5c31\u8fd4\u56de&#xff1b;\u6ca1\u627e\u5230\u8bf4\u660e\u5728\u672b\u5c3e&#xff0c;\u7ed5\u56de\u7b2c\u4e00\u4e2a\u8282\u70b9<br \/>\n     *\/<br \/>\n    std::string getNode(const std::string&amp; key) const {<br \/>\n        if (hash_ring_.empty()) {<br \/>\n            return &#034;&#034;;<br \/>\n        }<\/p>\n<p>        size_t key_hash &#061; std::hash&lt;std::string&gt;{}(key);<\/p>\n<p>        \/\/ lower_bound: \u627e\u7b2c\u4e00\u4e2a &gt;&#061; key_hash \u7684\u5143\u7d20<br \/>\n        auto it &#061; hash_ring_.lower_bound(key_hash);<\/p>\n<p>        if (it &#061;&#061; hash_ring_.end()) {<br \/>\n            \/\/ \u7ed5\u56de\u73af\u7684\u8d77\u70b9<br \/>\n            it &#061; hash_ring_.begin();<br \/>\n        }<\/p>\n<p>        return it-&gt;second;  \/\/ \u8fd4\u56de\u771f\u5b9e\u8282\u70b9\u540d<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * &#064;brief \u83b7\u53d6\u5f53\u524d\u865a\u62df\u8282\u70b9\u603b\u6570&#xff08;\u8c03\u8bd5\u7528&#xff09;<br \/>\n     *\/<br \/>\n    size_t size() const {<br \/>\n        return hash_ring_.size();<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int virtual_node_count_;       \/\/\/&lt; \u6bcf\u4e2a\u771f\u5b9e\u8282\u70b9\u7684\u865a\u62df\u8282\u70b9\u6570<\/p>\n<p>    \/**<br \/>\n     * \u54c8\u5e0c\u73af&#xff1a;\u6709\u5e8f map&#xff0c;key &#061; \u865a\u62df\u8282\u70b9\u54c8\u5e0c\u503c&#xff0c;value &#061; \u771f\u5b9e\u8282\u70b9\u540d<br \/>\n     * \u7528 std::map&#xff08;\u7ea2\u9ed1\u6811&#xff09;\u4fdd\u6301\u6709\u5e8f&#xff0c;\u65b9\u4fbf lower_bound \u67e5\u627e<br \/>\n     *\/<br \/>\n    std::map&lt;size_t, std::string&gt; hash_ring_;<br \/>\n};<\/p>\n<p>\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u6d4b\u8bd5 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\nint main() {<br \/>\n    ConsistentHash ch(200);  \/\/ \u6bcf\u4e2a\u771f\u5b9e\u8282\u70b9 200 \u4e2a\u865a\u62df\u8282\u70b9<\/p>\n<p>    \/\/ \u52a0 3 \u53f0\u670d\u52a1\u5668<br \/>\n    ch.addNode(&#034;192.168.1.10&#034;);<br \/>\n    ch.addNode(&#034;192.168.1.11&#034;);<br \/>\n    ch.addNode(&#034;192.168.1.12&#034;);<\/p>\n<p>    std::cout &lt;&lt; &#034;\u865a\u62df\u8282\u70b9\u603b\u6570: &#034; &lt;&lt; ch.size() &lt;&lt; std::endl;  \/\/ 600<\/p>\n<p>    \/\/ \u6d4b\u8bd5\u4e00\u4e9b key \u843d\u5728\u54ea\u4e2a\u8282\u70b9<br \/>\n    std::string keys[] &#061; {&#034;user:1&#034;, &#034;user:2&#034;, &#034;user:3&#034;, &#034;order:100&#034;, &#034;product:50&#034;};<\/p>\n<p>    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061; 3 \u53f0\u8282\u70b9\u65f6 &#061;&#061;&#061;&#034; &lt;&lt; std::endl;<br \/>\n    for (const auto&amp; k : keys) {<br \/>\n        std::cout &lt;&lt; k &lt;&lt; &#034; \u2192 &#034; &lt;&lt; ch.getNode(k) &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u52a0\u4e00\u53f0\u65b0\u670d\u52a1\u5668<br \/>\n    ch.addNode(&#034;192.168.1.13&#034;);<br \/>\n    std::cout &lt;&lt; &#034;\\\\n&#061;&#061;&#061; 4 \u53f0\u8282\u70b9\u65f6 &#061;&#061;&#061;&#034; &lt;&lt; std::endl;<br \/>\n    for (const auto&amp; k : keys) {<br \/>\n        std::cout &lt;&lt; k &lt;&lt; &#034; \u2192 &#034; &lt;&lt; ch.getNode(k) &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>\u56db\u3001\u5b9e\u6218\u573a\u666f<\/h3>\n<p>1. \u7f13\u5b58\u7cfb\u7edf<\/p>\n<p>Redis\u3001Memcached \u7b49\u5206\u5e03\u5f0f\u7f13\u5b58&#xff0c;\u672c\u8d28\u5c31\u662f\u8d85\u5927\u7684\u5206\u5e03\u5f0f\u6563\u5217\u8868&#xff0c;key-value \u5b58\u50a8&#xff0c;O (1) \u8bfb\u5199&#xff0c;\u662f\u540e\u7aef\u7cfb\u7edf\u6027\u80fd\u4f18\u5316\u7684\u6807\u914d\u3002<\/p>\n<p>\u672c\u5730\u7f13\u5b58&#xff08;Guava Cache\u3001Caffeine&#xff09;\u4e5f\u90fd\u662f\u57fa\u4e8e\u6563\u5217\u8868\u5b9e\u73b0\u7684\u3002<\/p>\n<p>2. \u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u5bb9\u5668<\/p>\n<p>\u51e0\u4e4e\u6240\u6709\u4e3b\u6d41\u8bed\u8a00\u7684\u5b57\u5178 \/ \u6620\u5c04\u5bb9\u5668\u90fd\u662f\u6563\u5217\u8868\u5b9e\u73b0&#xff1a;<\/p>\n<ul>\n<li>C&#043;&#043;&#xff1a;unordered_map\u3001unordered_set&#xff08;\u94fe\u5730\u5740\u6cd5&#xff09;<\/li>\n<li>Java&#xff1a;HashMap\u3001HashSet&#xff08;\u94fe\u5730\u5740\u6cd5 &#043; \u7ea2\u9ed1\u6811\u4f18\u5316&#xff09;<\/li>\n<li>Python&#xff1a;dict\u3001set&#xff08;\u5f00\u653e\u5bfb\u5740\u6cd5&#xff09;<\/li>\n<li>Go&#xff1a;map&#xff08;\u5f00\u653e\u5bfb\u5740\u6cd5\u53d8\u79cd&#xff09;<\/li>\n<\/ul>\n<p>3. \u6570\u636e\u5e93\u7d22\u5f15<\/p>\n<p>MySQL \u7684 Memory \u5b58\u50a8\u5f15\u64ce\u652f\u6301\u54c8\u5e0c\u7d22\u5f15&#xff0c;\u7b49\u503c\u67e5\u8be2 O (1)&#xff0c;\u6bd4 B&#043; \u6811\u8fd8\u5feb&#xff1b;\u4f46\u4e0d\u652f\u6301\u8303\u56f4\u67e5\u8be2\u3001\u6392\u5e8f&#xff0c;\u9002\u7528\u573a\u666f\u6709\u9650\u3002<\/p>\n<p>Redis \u7684\u5b57\u5178\u3001\u5185\u90e8\u5143\u6570\u636e\u7ba1\u7406&#xff0c;\u5168\u90e8\u57fa\u4e8e\u6563\u5217\u8868\u5b9e\u73b0\u3002<\/p>\n<p>4. \u53bb\u91cd\u4e0e\u8ba1\u6570<\/p>\n<p>\u6d77\u91cf\u6570\u636e\u53bb\u91cd\u3001\u8bcd\u9891\u7edf\u8ba1\u3001\u7528\u6237 UV \u7edf\u8ba1&#xff0c;\u90fd\u662f\u6563\u5217\u8868\u7684\u7ecf\u5178\u5e94\u7528&#xff1a;<\/p>\n<ul>\n<li>\u65e5\u5fd7\u5206\u6790&#xff1a;\u7edf\u8ba1\u6bcf\u4e2a\u63a5\u53e3\u7684\u8bbf\u95ee\u6b21\u6570<\/li>\n<li>\u722c\u866b&#xff1a;URL \u53bb\u91cd&#xff0c;\u907f\u514d\u91cd\u590d\u722c\u53d6<\/li>\n<li>\u63a8\u8350\u7cfb\u7edf&#xff1a;\u7528\u6237\u5386\u53f2\u884c\u4e3a\u53bb\u91cd&#xff0c;\u907f\u514d\u91cd\u590d\u63a8\u8350<\/li>\n<\/ul>\n<p>5. \u8def\u7531\u4e0e\u8d1f\u8f7d\u5747\u8861<\/p>\n<p>Nginx\u3001API \u7f51\u5173\u7684\u8def\u7531\u5339\u914d&#xff0c;\u7528\u6563\u5217\u8868\u5feb\u901f\u6839\u636e URL \u627e\u5230\u5bf9\u5e94\u7684\u540e\u7aef\u670d\u52a1&#xff1b;<\/p>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c\u8d1f\u8f7d\u5747\u8861&#xff0c;\u4fdd\u8bc1\u540c\u4e00\u7528\u6237\u7684\u8bf7\u6c42\u843d\u5230\u540c\u4e00\u53f0\u670d\u52a1\u5668&#xff0c;\u5b9e\u73b0\u4f1a\u8bdd\u4fdd\u6301\u3002<\/p>\n<p>6. \u7f16\u8bd1\u4e0e\u89e3\u91ca\u5668<\/p>\n<p>\u7f16\u8bd1\u5668\u7684\u7b26\u53f7\u8868&#xff0c;\u7528\u6563\u5217\u8868\u5b58\u50a8\u53d8\u91cf\u540d\u3001\u51fd\u6570\u540d\u5230\u5730\u5740\u7684\u6620\u5c04&#xff0c;\u67e5\u627e\u6781\u5feb&#xff0c;\u662f\u7f16\u8bd1\u8fc7\u7a0b\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u57fa\u7840\u6982\u5ff5\u7c7b\u6563\u5217\u8868&#xff08;Hash Table&#xff0c;\u4e5f\u53eb\u54c8\u5e0c\u8868&#xff09;&#xff0c;\u6838\u5fc3\u601d\u60f3\u662f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4&#xff1a;\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u628a key \u6620\u5c04\u6210\u6570\u7ec4\u4e0b\u6807&#xff0c;\u76f4\u63a5\u6309\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20&#xff0c;\u628a\u67e5\u627e\u590d\u6742\u5ea6\u4ece O (n) \u964d\u5230\u5e73\u5747 O (1)\u3002<br \/>\n\u6838\u5fc3\u672f\u8bed<br \/>\n\u54c8\u5e0c\u51fd\u6570&#xff1a;\u628a\u4efb\u610f\u957f\u5ea6\u7684 key&#xff0c;\u8f6c\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u4e0b\u6807\u7684\u51fd\u6570&#xff0c;\u8bb0\u4e3a hash(key)  index\u3002\u54c8\u5e0c\u51b2\u7a81&#xff1a;\u4e24\u4e2a<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3520,2872,1813],"topic":[],"class_list":["post-89751","post","type-post","status-publish","format-standard","hentry","category-server","tag-3520","tag-2872","tag-1813"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/89751.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u57fa\u7840\u6982\u5ff5\u7c7b\u6563\u5217\u8868&#xff08;Hash Table&#xff0c;\u4e5f\u53eb\u54c8\u5e0c\u8868&#xff09;&#xff0c;\u6838\u5fc3\u601d\u60f3\u662f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4&#xff1a;\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u628a key \u6620\u5c04\u6210\u6570\u7ec4\u4e0b\u6807&#xff0c;\u76f4\u63a5\u6309\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20&#xff0c;\u628a\u67e5\u627e\u590d\u6742\u5ea6\u4ece O (n) \u964d\u5230\u5e73\u5747 O (1)\u3002 \u6838\u5fc3\u672f\u8bed \u54c8\u5e0c\u51fd\u6570&#xff1a;\u628a\u4efb\u610f\u957f\u5ea6\u7684 key&#xff0c;\u8f6c\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u4e0b\u6807\u7684\u51fd\u6570&#xff0c;\u8bb0\u4e3a hash(key) index\u3002\u54c8\u5e0c\u51b2\u7a81&#xff1a;\u4e24\u4e2a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/89751.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-03T08:33:17+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89751.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/89751.html\",\"name\":\"\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-03T08:33:17+00:00\",\"dateModified\":\"2026-08-03T08:33:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/89751.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/89751.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89751.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5341\u4e8c.\u6563\u5217\u8868\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/89751.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u57fa\u7840\u6982\u5ff5\u7c7b\u6563\u5217\u8868&#xff08;Hash Table&#xff0c;\u4e5f\u53eb\u54c8\u5e0c\u8868&#xff09;&#xff0c;\u6838\u5fc3\u601d\u60f3\u662f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4&#xff1a;\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u628a key \u6620\u5c04\u6210\u6570\u7ec4\u4e0b\u6807&#xff0c;\u76f4\u63a5\u6309\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20&#xff0c;\u628a\u67e5\u627e\u590d\u6742\u5ea6\u4ece O (n) \u964d\u5230\u5e73\u5747 O (1)\u3002 \u6838\u5fc3\u672f\u8bed \u54c8\u5e0c\u51fd\u6570&#xff1a;\u628a\u4efb\u610f\u957f\u5ea6\u7684 key&#xff0c;\u8f6c\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u4e0b\u6807\u7684\u51fd\u6570&#xff0c;\u8bb0\u4e3a hash(key) index\u3002\u54c8\u5e0c\u51b2\u7a81&#xff1a;\u4e24\u4e2a","og_url":"https:\/\/www.wsisp.com\/helps\/89751.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-03T08:33:17+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/89751.html","url":"https:\/\/www.wsisp.com\/helps\/89751.html","name":"\u5341\u4e8c.\u6563\u5217\u8868 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-03T08:33:17+00:00","dateModified":"2026-08-03T08:33:17+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/89751.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/89751.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/89751.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5341\u4e8c.\u6563\u5217\u8868"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=89751"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89751\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=89751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=89751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=89751"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=89751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}