-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs__algorithm__datasources__hash.md.js
More file actions
1 lines (1 loc) · 4 KB
/
docs__algorithm__datasources__hash.md.js
File metadata and controls
1 lines (1 loc) · 4 KB
1
(window["webpackJsonp"]=window["webpackJsonp"]||[]).push([[6],{"x/y/":function(n,e,t){"use strict";t.r(e);var a=t("q1tI"),l=t.n(a),i=t("dEAq"),s=t("H1Ra"),h=l.a.memo((n=>{n.demos;return l.a.createElement(l.a.Fragment,null,l.a.createElement("div",{className:"markdown"},l.a.createElement("h2",{id:"\u6563\u5217\u7684\u6982\u5ff5"},l.a.createElement(i["AnchorLink"],{to:"#\u6563\u5217\u7684\u6982\u5ff5","aria-hidden":"true",tabIndex:-1},l.a.createElement("span",{className:"icon icon-link"})),"\u6563\u5217\u7684\u6982\u5ff5"),l.a.createElement("ul",null,l.a.createElement("li",null,"\u6563\u5217\u540e\u7684\u6570\u636e\u53ef\u4ee5\u5feb\u901f\u63d2\u5165\u53d6\u7528"),l.a.createElement("li",null,"\u5728\u6563\u5217\u8868\u4e0a\u63d2\u5165\u3001\u5220\u9664\u548c\u53d6\u7528\u6570\u636e\u975e\u5e38\u5feb\uff0c",l.a.createElement("strong",null,"\u67e5\u627e\u6570\u636e\u5374\u6548\u7387\u4f4e\u4e0b"),"\uff0c\u6bd4\u5982\u67e5\u627e\u4e00\u7ec4\u6570\u636e\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c"),l.a.createElement("li",null,"javascript \u6563\u5217\u8868\u57fa\u4e8e\u6570\u7ec4\u8bbe\u8ba1\uff0c\u7406\u60f3\u60c5\u51b5\u6563\u5217\u51fd\u6570\u4f1a\u5c06\u6bcf\u4e2a\u952e\u503c\u6620\u5c04\u4e3a\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u6570\u7ec4\u957f\u5ea6\u6709\u9650\u5236\uff0c\u66f4\u73b0\u5b9e\u7684\u7b56\u7565\u662f\u5c06\u952e\u5747\u5300\u5206\u5e03"),l.a.createElement("li",null,"\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u5feb\u9012")),l.a.createElement("h2",{id:"\u4ee3\u7801\u5b9e\u73b0"},l.a.createElement(i["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0","aria-hidden":"true",tabIndex:-1},l.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),l.a.createElement(s["a"],{code:"function HashTable() {\n this.table = new Array(137); //\u907f\u514d\u78b0\u649e\u7684\u7b2c\u4e00\u4e2a\u8d28\u6570\n this.simpleHash = simpleHash; //\u8ba1\u7b97\u6563\u5217\u503c\u7684\u65b9\u6cd5 \u78b0\u649e\u6982\u7387\u6bd4\u8f83\u5927\n this.betterHash = betterHash; //\u970d\u7eb3\u7b97\u6cd5\n this.put = put;\n this.get = get;\n this.showDis = showDis;\n this.buildChians = buildChians;\n}\n\n//\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\u6570\u7ec4\nfunction buildChians() {\n for (var i = 0; i < data.length; i++) {\n this.table[i] = new Array();\n }\n}\n\n//\u9664\u7559\u4f59\u6570\u6cd5\nfunction simpleHash(data) {\n var total = 0;\n for (let i = 0; i < data.length; i++) {\n total += data.charCodeAt(i);\n }\n return total % this.table.length;\n}\n\n// \u66f4\u597d\u7684\u5206\u914d\u952e\u503c\nfunction betterHash(data) {\n var H = 31;\n var total = 0;\n for (let i = 0; i < data.length; i++) {\n total += H * total + data.charCodeAt(i);\n }\n if (total < 0) {\n total += this.table.length - 1;\n }\n return total % this.table.length;\n}\n\n//\u63d2\u5165 \u7ebf\u6027\u63a2\u6d4b\u6cd5\nfunction put(data) {\n var pos = this.simpleHash(data);\n if (this.table[pos] == undefined) {\n this.table[pos] = data;\n } else {\n while (this.table[pos] != undefined) {\n pos++;\n }\n this.table[pos] = data;\n }\n // this.table[pos]=data\n}\n\n// \u83b7\u53d6\nfunction get(key) {\n var hash = this.simpleHash(data);\n console.log(hash);\n for (let i = hash; i < this.table.length; i++) {\n if (this.table[i] == key) {\n return i;\n }\n }\n return undefined;\n}\n\n//\u663e\u793a\nfunction showDis() {\n var n = 0;\n for (let i = 0; i < this.table.length; i++) {\n if (this.table[i] != undefined) {\n console.log('\u952e\u503c\u662f->' + i + ' \u503c\u662f' + this.table[i]);\n }\n }\n}\n\nvar hTable = new HashTable();\nhTable.put('china');\nhTable.put('Japan');\nhTable.put('America');\nhTable.put('nicha');\nconsole.log(hTable);\nhTable.showDis();",lang:"js"})))}));e["default"]=n=>{var e=l.a.useContext(i["context"]),t=e.demos;return l.a.useEffect((()=>{var e;null!==n&&void 0!==n&&null!==(e=n.location)&&void 0!==e&&e.hash&&i["AnchorLink"].scrollToAnchor(decodeURIComponent(n.location.hash.slice(1)))}),[]),l.a.createElement(h,{demos:t})}}}]);