{"id":293,"date":"2019-02-24T16:22:08","date_gmt":"2019-02-24T08:22:08","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=293"},"modified":"2019-02-24T16:22:08","modified_gmt":"2019-02-24T08:22:08","slug":"%e4%bd%bf%e7%94%a8golang%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e5%a4%a7%e9%a1%b6%e5%a0%86","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=293","title":{"rendered":"\u4f7f\u7528golang\u5b9e\u73b0\u4e00\u4e2a\u5927\u9876\u5806"},"content":{"rendered":"<h1>1 heap\u662f\u4ec0\u4e48<\/h1>\n<p>\u5806\uff08Heap\uff09\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u4e00\u7c7b\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\u7684\u7edf\u79f0\u3002\u5806\u901a\u5e38\u662f\u4e00\u4e2a\u53ef\u4ee5\u88ab\u770b\u505a\u4e00\u68f5\u6811\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u5728\u961f\u5217\u4e2d\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u53cd\u590d\u63d0\u53d6\u961f\u5217\u4e2d\u7b2c\u4e00\u4e2a\u4f5c\u4e1a\u5e76\u8fd0\u884c\uff0c\u56e0\u4e3a\u5b9e\u9645\u60c5\u51b5\u4e2d\u67d0\u4e9b\u65f6\u95f4\u8f83\u77ed\u7684\u4efb\u52a1\u5c06\u7b49\u5f85\u5f88\u957f\u65f6\u95f4\u624d\u80fd\u7ed3\u675f\uff0c\u6216\u8005\u67d0\u4e9b\u4e0d\u77ed\u5c0f\uff0c\u4f46\u5177\u6709\u91cd\u8981\u6027\u7684\u4f5c\u4e1a\uff0c\u540c\u6837\u5e94\u5f53\u5177\u6709\u4f18\u5148\u6743\u3002\u5806\u5373\u4e3a\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u8bbe\u8ba1\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<h1>2 heap\u53ef\u4ee5\u7528\u6765\u505a\u4ec0\u4e48<\/h1>\n<p>\u4f18\u5148\u7ea7\u961f\u5217\u3001\u6c42 Top K \u548c\u6c42\u4e2d\u4f4d\u6570<\/p>\n<h1>3 Go\u6807\u51c6\u5e93\u4e2d\u7684\u5b9e\u73b0<\/h1>\n<p>Go \u6807\u51c6\u5e93\u4e2d container\/heap \u539f\u6587\u5730\u5740\u4e3a\uff1a https:\/\/golang.org\/pkg\/container\/heap\/<\/p>\n<h1>4 \u52a8\u624b\u5199\u4e00\u4e2a\u5927\u9876\u5806<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"> \r\n\r\npackage src\r\n\r\n\/\/&quot;container\/heap&quot;\uff09\r\n\r\ntype BigHeap struct {\r\n\tHeapList []int\r\n\tMaxNum   int\r\n\tCount    int\r\n}\r\n\r\nfunc (h *BigHeap) Insert(data int) {\r\n\tif h.Count &gt;= h.MaxNum {\r\n\t\treturn\r\n\t}\r\n\th.Count += 1\r\n\th.HeapList[h.Count] = data\r\n\ti := h.Count\r\n\tfor i\/2 &gt; 0 &amp;&amp; h.HeapList[i] &gt; h.HeapList[i\/2] {\r\n\t\th.swap(i, i\/2)\r\n\t\ti = i \/ 2\r\n\t}\r\n}\r\n\r\nfunc (h *BigHeap) swap(a, b int) {\r\n\th.HeapList[a], h.HeapList[b] = h.HeapList[b], h.HeapList[a]\r\n\r\n}\r\n\r\nfunc (h *BigHeap) RemoveMax() {\r\n\tif h.Count == 0 {\r\n\t\treturn\r\n\t}\r\n\th.HeapList[1] = h.HeapList[h.Count]\r\n\th.HeapList[h.Count] = 0\r\n\th.Count -= 1\r\n\th.heapify(h.Count, 1)\r\n\r\n}\r\n\r\nfunc (h *BigHeap) heapify(n, i int) { \/\/ \u81ea\u4e0a\u5f80\u4e0b\u5806\u5316\r\n\tfor {\r\n\t\tmaxPos := i\r\n\t\tif i*2 &lt;= n &amp;&amp; h.HeapList[i] &lt; h.HeapList[i*2] {\r\n\t\t\tmaxPos = i * 2\r\n\t\t}\r\n\t\tif i*2+1 &lt;= n &amp;&amp; h.HeapList[maxPos] &lt; h.HeapList[i*2+1] {\r\n\t\t\tmaxPos = i*2 + 1\r\n\t\t}\r\n\t\tif maxPos == i {\r\n\t\t\tbreak\r\n\t\t}\r\n\t\th.swap(i, maxPos)\r\n\t\ti = maxPos\r\n\t}\r\n\r\n}\r\n\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1 heap\u662f\u4ec0\u4e48 \u5806\uff08Heap\uff09\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u4e00\u7c7b\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\u7684\u7edf\u79f0\u3002\u5806\u901a\u5e38\u662f\u4e00\u4e2a\u53ef\u4ee5\u88ab\u770b\u505a\u4e00\u68f5\u6811\u7684\u6570\u7ec4\u5bf9<a href=\"https:\/\/www.ccagml.com\/?p=293\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u4f7f\u7528golang\u5b9e\u73b0\u4e00\u4e2a\u5927\u9876\u5806<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/293"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=293"}],"version-history":[{"count":5,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions\/300"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}