{"id":143,"date":"2018-12-04T10:53:14","date_gmt":"2018-12-04T02:53:14","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=143"},"modified":"2021-04-24T17:02:09","modified_gmt":"2021-04-24T09:02:09","slug":"design_hashmap_i","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2018\/12\/04\/design_hashmap_i\/","title":{"rendered":"\u6784\u9020\u54c8\u5e0c\u8868I"},"content":{"rendered":"<p><script type=\"text\/javascript\" async src=\"https:\/\/www.caiqinyi.cn\/wp-content\/MathJax\/MathJax.js?config=TeX-AMS_CHTML\">\n<\/script><br \/>\n<script type=\"text\/x-mathjax-config\">\n    MathJax.Hub.Config({\n        tex2jax: {inlineMath: [['$','$']]},\n        TeX: {equationNumbers: {autoNumber: [\"AMS\"], useLabelIds: true}},\n        \"HTML-CSS\": {linebreaks: {automatic: true}},\n        SVG: {linebreaks: {automatic: true}}\n    });\n<\/script><\/p>\n<p>\u5982\u679c\u5728\u4e0d\u7528C++\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6, \u4f60\u4f1a\u600e\u4e48\u6784\u9020\u4e00\u4e2a\u54c8\u5e0c\u8868\u5462? \u5f53\u7136, \u6b64\u65f6\u952e\u503c\u662f\u6709\u9650\u7684, \u5373\u6709\u4e0a\u754c\u548c\u4e0b\u754c. \u6709\u4e00\u4e2a\u8fd9\u4e48\u597d\u7684\u6761\u4ef6\u9650\u5236, \u6211\u81ea\u7136\u800c\u7136\u5730\u60f3\u5230\u4e86\u7528\u4e00\u4e2avector\u987a\u5e8f\u8bbf\u95ee, \u4ee3\u7801\u5982\u4e0b:<\/p>\n<p><!--more--><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\nclass MyHashMap {\r\nprivate:\r\n    vector&lt;int&gt; hash;\r\npublic:\r\n    \/** Initialize your data structure here. *\/\r\n    MyHashMap() {\r\n        hash.resize(1000001,-1);\r\n    }\r\n    \r\n    \/** value will always be non-negative. *\/\r\n    void put(int key, int value) {\r\n        hash[key]=value;\r\n    }\r\n    \r\n    \/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key *\/\r\n    int get(int key) {\r\n        return hash[key];\r\n    }\r\n    \r\n    \/** Removes the mapping of the specified value key if this map contains a mapping for the key *\/\r\n    void remove(int key) {\r\n        hash[key]=-1;\r\n    }\r\n};\r\n\r\n\/**\r\n * Your MyHashMap object will be instantiated and called as such:\r\n * MyHashMap obj = new MyHashMap();\r\n * obj.put(key,value);\r\n * int param_2 = obj.get(key);\r\n * obj.remove(key);\r\n *\/\r\n<\/pre>\n<p>\u4f46\u6211\u8dd1\u4e8633\u4e2a\u6848\u4f8b\u4ee5\u540e, \u53d1\u73b0\u8017\u65f6\u9ad8\u8fbe80ms, \u8fd9\u5b9e\u5728\u662f\u4ee4\u6211\u6709\u70b9\u96be\u4ee5\u63a5\u53d7, \u6309\u7406\u8bf4\u90fd\u662fO(1)\u7684\u64cd\u4f5c, \u8017\u65f6\u4e0d\u5e94\u8be5\u8fd9\u4e48\u9ad8\u624d\u5bf9\u2026\u2026 \u770b\u5230\u4e00\u4e2a\u5927\u795e\u7528\u94fe\u8868\u6784\u9020\u54c8\u5e0c\u8868\u7684\u4ee3\u7801, \u8017\u65f6\u4ec560ms, mark\u4e00\u4e0b\u5148:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\nconst static auto io_speed_up=[](){\r\n    ios::sync_with_stdio(false);\r\n    cin.tie(NULL);\r\n    cout.tie(NULL);\r\n    return NULL;\r\n}();\r\n\r\nclass MyHashMap {\r\npublic:\r\n    \/** Initialize your data structure here. *\/\r\n    list&lt;pair&lt;int, int&gt;&gt; buckets[1000];\r\n    MyHashMap() {\r\n        \r\n    }\r\n    \/** value will always be non-negative. *\/\r\n    void put(int key, int value) {\r\n        bool flag = false;\r\n        int hash = (key*1031)%1000;\r\n        \/\/printf(\"%s: key,value,hash(%d %d %d)\\n\", __func__, key, value, hash);\r\n        list&lt;pair&lt;int, int&gt;&gt; &li = buckets[hash];\r\n        for (auto it = li.begin(); it != li.end(); it++) {\r\n            if (it->first == key) {\r\n                flag = true;\r\n                it->second = value;\r\n            }\r\n        }\r\n        if (!flag) {\r\n            li.push_back(make_pair(key, value));\r\n        }\r\n    }\r\n    \r\n    \/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key *\/\r\n    int get(int key) {\r\n        int hash = (key*1031)%1000;\r\n        \/\/printf(\"%s: key,hash(%d %d)\\n\", __func__, key, hash);\r\n        list&lt;pair&lt;int, int&gt;&gt; li = buckets[hash];\r\n        for (auto it = li.begin(); it != li.end(); it++) {\r\n            \/\/printf(\"key, value(%d %d)\\n\", it->first, it->second);\r\n            if (it->first == key) return it->second;\r\n        }\r\n        return -1;\r\n    }\r\n    \r\n    \/** Removes the mapping of the specified value key if this map contains a mapping for the key *\/\r\n    void remove(int key) {\r\n        \r\n        int hash = (key*1031)%1000;\r\n        list&lt;pair&lt;int, int&gt;&gt; &li = buckets[hash];\r\n        for (auto it = li.begin(); it != li.end(); it++) {\r\n            if (it->first == key) {\r\n                li.erase(it);\r\n                return;\r\n            }\r\n        }\r\n    }\r\n};\r\n\r\n\/**\r\n * Your MyHashMap object will be instantiated and called as such:\r\n * MyHashMap obj = new MyHashMap();\r\n * obj.put(key,value);\r\n * int param_2 = obj.get(key);\r\n * obj.remove(key);\r\n *\/\r\n<\/pre>\n<p>\u4ed6\u5229\u7528\u8d28\u65701031\u53bb\u8ba1\u7b97\u51fa\u4e00\u4e2a\u54c8\u5e0c\u503c, \u540c\u65f6\u518d\u53d6\u9664\u4ee51000\u7684\u4f59\u6570. \u8fd9\u6837\u53ef\u4ee5\u5f97\u52301000\u4e2a\u6876, \u7528\u4ee5\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\u7684\u95ee\u9898, \u5728\u53d6\u503c\u65f6, \u5148\u5b9a\u4f4d\u6570\u636e\u653e\u5728\u4e86\u54ea\u4e2a\u6876, \u518d\u904d\u5386\u94fe\u8868, \u76f8\u6bd4\u8f83\u76f4\u63a5\u904d\u5386\u6574\u4e2a\u94fe\u8868\u800c\u8a00, \u5927\u5927\u51cf\u5c11\u4e86\u8017\u65f6, \u4e0d\u80fd\u4e0d\u8bf4\u5b9e\u5728\u662f\u5999\u2026\u2026 (\u867d\u7136\u6211\u6700\u540e\u8fd8\u662f\u6ca1\u60f3\u660e\u767d\u8fd9\u4e48\u590d\u6742\u7684\u64cd\u4f5c\u4e3a\u4f55\u8017\u65f6\u6bd4\u6211\u4f4e\u5c31\u662f\u4e86QAQ\u2026\u2026)<\/p>\n<p>\u4e0b\u9762\u7ed9\u51fade\u795e\u5bf9\u4e8e\u4e3a\u4f55\u80fd\u7528$(key*1031)%1000$\u6765\u4f5c\u4e3a\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u7684\u539f\u56e0: \u4ece\u540c\u4f59\u65b9\u7a0b\u7684\u89d2\u5ea6\u6765\u7406\u89e3, \u82e5\u8981\u4f7f\u5f97$p*(key1-key2)\\equiv 0 mod M,M=1000$\u6210\u7acb\uff0c\u90a3\u4e48$p*(key1 &#8211; key2)  = t* M$, \u7531\u4e8e$(p,M)=1$, \u90a3\u4e48\u5fc5\u6709$M|(key1-key2)$. \u4e5f\u5c31\u662f\u8bf4, \u82e5\u540c\u4f59\u65b9\u7a0b\u8981\u6210\u7acb\u7684\u8bdd, $key1 = key2 + M*h$, \u8fd9\u4e9b\u6570\u662f\u5f88\u5c11\u7684, \u5bf9\u4e8e\u8fde\u7eed\u7684$[1,n]$, \u8fd9\u6837\u7684\u6570\u91cc\u9762, \u6bcf\u9694$M$\u4e2a\u6570\u624d\u6709\u4e00\u4e2a, \u4e5f\u5c31\u662f\u8fd9\u4e2a\u54c8\u5e0c\u6bcf\u9694$M$\u4e2a\u6570\u624d\u6709\u53ef\u80fd\u51b2\u7a81.  \u6b64\u5904\u7531\u4e8e1031>1000, \u90a3\u4e48\u7528$(key*1031)%1000$\u6765\u4f5c\u4e3a\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570\u4e5f\u5c31\u4e0d\u4f1a\u4ea7\u751f\u51b2\u7a81\u4e86~<\/p>\n<p>PS: \u8fd9\u4e2a\u6280\u5de7\u8fd8\u53ef\u4ee5\u7528\u4e8eDijkstra\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u5728\u4e0d\u7528C++\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6, \u4f60\u4f1a\u600e\u4e48\u6784\u9020\u4e00\u4e2a\u54c8\u5e0c\u8868\u5462? \u5f53\u7136, \u6b64\u65f6\u952e\u503c\u662f\u6709\u9650\u7684, \u5373\u6709\u4e0a\u754c\u548c\u4e0b\u754c. \u6709 &hellip; <a href=\"https:\/\/www.caiqinyi.cn\/index.php\/2018\/12\/04\/design_hashmap_i\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u6784\u9020\u54c8\u5e0c\u8868I<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":10,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}