{"id":258,"date":"2022-03-18T21:43:47","date_gmt":"2022-03-18T21:43:47","guid":{"rendered":"http:\/\/www.gislxz.top\/?p=258"},"modified":"2023-03-26T12:45:40","modified_gmt":"2023-03-26T12:45:40","slug":"leetcode-347-%e5%89%8dk%e4%b8%aa%e9%ab%98%e9%a2%91%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/www.gislxz.com\/index.php\/2022\/03\/18\/leetcode-347-%e5%89%8dk%e4%b8%aa%e9%ab%98%e9%a2%91%e5%85%83%e7%b4%a0\/","title":{"rendered":"Leetcode-347 \u524dk\u4e2a\u9ad8\u9891\u5143\u7d20"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"687\" src=\"http:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-1024x687.png\" alt=\"\" class=\"wp-image-259\" srcset=\"https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-1024x687.png 1024w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-300x201.png 300w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-768x515.png 768w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image.png 1198w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u9898\u76ee<\/figcaption><\/figure>\n\n\n\n<p>\u9898\u76ee\u5e76\u4e0d\u96be\uff0c\u9996\u5148\u904d\u5386\u4e00\u6b21\u6570\u7ec4\uff0c\u7528\u54c8\u5e0c\u8868\u8ba1\u6570\uff0c\u7136\u540e\u5bf9\u6b21\u6570\u6392\u5e8f\uff0c\u8fd4\u56de\u524dk\u4e2a\u503c\u5c31\u884c\u3002<\/p>\n\n\n\n<p>\u8bb0\u5f55\u4e00\u4e0bunordered_map\u7684\u7528\u6cd5\u65b9\u4fbf\u4ee5\u540e\u67e5\u9605\u3002<\/p>\n\n\n\n<p>\u9996\u5148unordered_map\u4e0emap\u7684\u533a\u522b\u5728\u4e8e\u662f\u5426\u6392\u5e8f\uff0c\u524d\u8005\u5b9e\u73b0\u662f\u54c8\u5e0c\u8868\uff0c\u540e\u8005\u5b9e\u73b0\u662f\u7ea2\u9ed1\u6811\uff0c\u5176\u4ed6\u51fd\u6570\u57fa\u672c\u76f8\u540c\uff0c\u90fd\u662f\u7528pair\u4f5c\u4e3a\u5b58\u50a8\u5355\u5143\u3002<\/p>\n\n\n\n<p>pair\u7ed3\u6784\u76f8\u5bf9\u7b80\u5355\uff0c\u5c31\u662f\u4e24\u4e2a\u503c\u7ec4\u6210\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pair&lt;T1, T2&gt; p1;            \/\/\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684pair\u5bf9\u8c61\uff08\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\uff09\uff0c\u5b83\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662fT1\u548cT2\u7c7b\u578b\uff0c\u91c7\u7528\u503c\u521d\u59cb\u5316\u3002\npair&lt;T1, T2&gt; p1(v1, v2);    \/\/\u521b\u5efa\u4e00\u4e2apair\u5bf9\u8c61\uff0c\u5b83\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662fT1\u548cT2\u7c7b\u578b\uff0c\u5176\u4e2dfirst\u6210\u5458\u521d\u59cb\u5316\u4e3av1\uff0csecond\u6210\u5458\u521d\u59cb\u5316\u4e3av2\u3002\nmake_pair(v1, v2);          \/\/ \u4ee5v1\u548cv2\u7684\u503c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684pair\u5bf9\u8c61\uff0c\u5176\u5143\u7d20\u7c7b\u578b\u5206\u522b\u662fv1\u548cv2\u7684\u7c7b\u578b\u3002\np1 &lt; p2;                    \/\/ \u4e24\u4e2apair\u5bf9\u8c61\u95f4\u7684\u5c0f\u4e8e\u8fd0\u7b97\uff0c\u5176\u5b9a\u4e49\u9075\u5faa\u5b57\u5178\u6b21\u5e8f\uff1a\u5982 p1.first &lt; p2.first \u6216\u8005 !(p2.first &lt; p1.first) &amp;&amp; (p1.second &lt; p2.second) \u5219\u8fd4\u56detrue\u3002\np1 == p2\uff1b                  \/\/ \u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684first\u548csecond\u4f9d\u6b21\u76f8\u7b49\uff0c\u5219\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\uff1b\u8be5\u8fd0\u7b97\u4f7f\u7528\u5143\u7d20\u7684==\u64cd\u4f5c\u7b26\u3002\np1.first;                   \/\/ \u8fd4\u56de\u5bf9\u8c61p1\u4e2d\u540d\u4e3afirst\u7684\u516c\u6709\u6570\u636e\u6210\u5458\np1.second;                 \/\/ \u8fd4\u56de\u5bf9\u8c61p1\u4e2d\u540d\u4e3asecond\u7684\u516c\u6709\u6570\u636e\u6210\u5458<\/code><\/pre>\n\n\n\n<p>unordered_map\u8be6\u7ec6\u7528\u6cd5\u89c1<a href=\"https:\/\/www.cnblogs.com\/langyao\/p\/8823092.html\" target=\"_blank\"  rel=\"nofollow\" >\u8be6\u7ec6\u4ecb\u7ecdC++STL\uff1aunordered_map - \u6724\u5c27 - \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    vector&lt;int&gt; topKFrequent(vector&lt;int&gt;&amp; nums, int k) {\n        unordered_map&lt;int,int&gt; times;\n        for(auto i: nums){\n            if(times.find(i)==times.end())\n                times.insert({i,1});\n            else\n                times&#91;i]++;\n        }\n        vector&lt;pair&lt;int,int&gt;&gt; tmp;\n        for(auto i:times)\n            tmp.push_back(i);\n        sort(tmp.begin(), tmp.end(), &#91;=](std::pair&lt;int, int&gt;&amp; a, std::pair&lt;int, int&gt;&amp; b) { return a.second &gt; b.second; });\n        vector&lt;int&gt; answer;\n        for(int i=0;i&lt;k;i++){\n            answer.push_back(tmp&#91;i].first);\n        }\n        return answer;\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u5230\u5bf9unordered_map\u6392\u5e8f\u53ef\u4ee5\u7528vector\u7684sort\uff0c\u5e76\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u3002\u53ef\u4ee5\u76f4\u63a5\u5199\u5728sort\u53c2\u6570\u91cc\uff0c\u4e5f\u53ef\u4ee5\u5728\u5916\u90e8\u5199\u597d\uff0c\u5c06\u51fd\u6570\u540d\u4f20\u8fdb\u53bb\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc[=]lambda\u51fd\u6570\u7684\u53d8\u91cf\u5f15\u7528\u65b9\u5f0f\uff0c\u8be6\u89c1<a href=\"https:\/\/www.cnblogs.com\/jimodetiantang\/p\/9016826.html\" target=\"_blank\"  rel=\"nofollow\" >C++\u4e4bLambda\u8868\u8fbe\u5f0f - \u5b63\u672b\u7684\u5929\u5802 - \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"616\" height=\"297\" src=\"http:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-1.png\" alt=\"\" class=\"wp-image-260\" srcset=\"https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-1.png 616w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-1-300x145.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u6837\u5199\u95ee\u9898\u4e0d\u5927\uff0c\u4f46\u6548\u7387\u6d6a\u8d39\u5728\u6211\u4eec\u53ea\u8981\u524dk\u4e2a\u503c\uff0c\u5e76\u4e0d\u9700\u8981\u5bf9\u6574\u4e2a\u6570\u7ec4\u6392\u5e8f\uff0c\u8fd9\u79cd\u53ea\u8981\u6392\u5e8f\u524dk\u4e2a\u503c\u7684\u60c5\u51b5\u5f88\u9002\u5408\u7528\u5806\u6765\u5b9e\u73b0\uff0c\u8fd9\u9898\u662f\u6b63\u5e8f\u5c31\u7528\u5927\u9876\u5806\u3002<\/p>\n\n\n\n<p>\u5927\u9876\u5806\u4e4b\u524d\u7684\u6587\u7ae0\u4e5f\u5199\u8fc7\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4f46\u6ca1\u5199stl\u5e93\u91cc\u7684priority_queue\u7528\u6cd5\uff0c\u56e0\u6b64\u8fd9\u91cc\u8865\u4e0a\u3002<\/p>\n\n\n\n<p>\u5e38\u89c4\u7684priority_queue\u7684\u5b9a\u4e49\u5982\u4e0b<br>Type \u5c31\u662f\u6570\u636e\u7c7b\u578b\uff0cContainer \u5c31\u662f\u5bb9\u5668\u7c7b\u578b\uff08Container\u5fc5\u987b\u662f\u7528\u6570\u7ec4\u5b9e\u73b0\u7684\u5bb9\u5668\uff0c\u6bd4\u5982vector,deque\u7b49\u7b49\uff0c\u4f46\u4e0d\u80fd\u7528 list\u3002STL\u91cc\u9762\u9ed8\u8ba4\u7528\u7684\u662fvector\uff09\uff0cFunctional \u5c31\u662f\u6bd4\u8f83\u7684\u65b9\u5f0f\uff0c\u5f53\u9700\u8981\u7528\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u65f6\u624d\u9700\u8981\u4f20\u5165\u8fd9\u4e09\u4e2a\u53c2\u6570\uff0c\u4f7f\u7528\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u53ea\u9700\u8981\u4f20\u5165\u6570\u636e\u7c7b\u578b\uff0c\u9ed8\u8ba4\u662f\u5927\u9876\u5806<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5347\u5e8f\u961f\u5217\npriority_queue &lt;int,vector&lt;int&gt;,greater&lt;int&gt; &gt; q;\n\/\/\u964d\u5e8f\u961f\u5217\npriority_queue &lt;int,vector&lt;int&gt;,less&lt;int&gt; &gt;q;\n\n\/\/greater\u548cless\u662fstd\u5b9e\u73b0\u7684\u4e24\u4e2a\u4eff\u51fd\u6570\uff08\u5c31\u662f\u4f7f\u4e00\u4e2a\u7c7b\u7684\u4f7f\u7528\u770b\u4e0a\u53bb\u50cf\u4e00\u4e2a\u51fd\u6570\u3002\u5176\u5b9e\u73b0\u5c31\u662f\u7c7b\u4e2d\u5b9e\u73b0\u4e00\u4e2aoperator()\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u6709\u4e86\u7c7b\u4f3c\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u5c31\u662f\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b\u4e86\uff09\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u8fd9\u9898\u6211\u4eec\u9700\u8981\u5c06\u5806\u7684\u5143\u7d20\u6539\u4e3apair\uff0c\u4f46\u4f7f\u7528pair\u7684\u4f18\u5148\u961f\u5217\u7684\u6bd4\u8f83\u51fd\u6570\u662f\u5148\u6bd4\u8f83first\u518d\u6bd4\u8f83second\uff0c\u56e0\u6b64\u8fd8\u9700\u8981\u91cd\u5199\u6bd4\u8f83\u51fd\u6570\u3002\u53c2\u8003\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u4f18\u5148\u961f\u5217\u65b9\u6cd5\u5b9a\u4e49\u7b26\u5408\u8981\u6c42\u7684priority_queue<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\n\/\/\u65b9\u6cd51\nstruct tmp1 \/\/\u8fd0\u7b97\u7b26\u91cd\u8f7d&lt;\n{\n    int x;\n    tmp1(int a) {x = a;}\n    bool operator&lt;(const tmp1&amp; a) const\n    {\n        return x &lt; a.x; \/\/\u5927\u9876\u5806\n    }\n};\n\n\/\/\u65b9\u6cd52\nstruct tmp2 \/\/\u91cd\u5199\u4eff\u51fd\u6570\n{\n    bool operator() (tmp1 a, tmp1 b) \n    {\n        return a.x &lt; b.x; \/\/\u5927\u9876\u5806\n    }\n};\n\nint main() \n{\n    tmp1 a(1);\n    tmp1 b(2);\n    tmp1 c(3);\n    priority_queue&lt;tmp1&gt; d;\n    d.push(b);\n    d.push(c);\n    d.push(a);\n    while (!d.empty()) \n    {\n        cout &lt;&lt; d.top().x &lt;&lt; '\\n';\n        d.pop();\n    }\n    cout &lt;&lt; endl;\n\n    priority_queue&lt;tmp1, vector&lt;tmp1&gt;, tmp2&gt; f;\n    f.push(c);\n    f.push(b);\n    f.push(a);\n    while (!f.empty()) \n    {\n        cout &lt;&lt; f.top().x &lt;&lt; '\\n';\n        f.pop();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5c06\u89e3\u6cd5\u8fdb\u884c\u4fee\u6539<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    static bool cmp(pair&lt;int,int&gt; &amp;a,pair&lt;int,int&gt; &amp;b){return a.second&gt;b.second;}\/\/static\u9650\u5236\u51fd\u6570\u4f5c\u7528\u8303\u56f4\n    vector&lt;int&gt; topKFrequent(vector&lt;int&gt;&amp; nums, int k) {\n        unordered_map&lt;int,int&gt; times;\n        for(auto i: nums){\n            if(times.find(i)==times.end())\n                times.insert({i,1});\n            else\n                times&#91;i]++;\n        }\n        priority_queue&lt;pair&lt;int,int&gt;,vector&lt;pair&lt;int,int&gt;&gt;,decltype(&amp;cmp)&gt; temp(cmp);\n        for(auto i:times){\n            if(temp.size()&lt;k)\n                temp.push(i);\n            else{\n                if(i.second&gt;temp.top().second){\n                    temp.pop();\n                    temp.push(i);\n                }\n            }\n        }\n        vector&lt;int&gt; answer;\n        while(!temp.empty()){\n            answer.push_back(temp.top().first);\n            temp.pop();\n        }\n        return answer;\n    }\n};<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"307\" src=\"http:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-2.png\" alt=\"\" class=\"wp-image-261\" srcset=\"https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-2.png 645w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2022\/03\/image-2-300x143.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><figcaption>\u53cd\u5411\u4f18\u5316\u4e86\u5c5e\u4e8e\u662f<\/figcaption><\/figure>\n\n\n\n<p>\u81f3\u4e8e\u4eff\u51fd\u6570\uff0cdecltype\u7b49\u641e\u61c2\u540e\u5728\u66f4\u5427<\/p>\n\n\n\n<p>\u8fd8\u662f\u5199\u4e2astruct\u6613\u61c2\u4e00\u4e9b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    vector&lt;int> topKFrequent(vector&lt;int>&amp; nums, int k) {\r\n    \/\/1.map\u8bb0\u5f55\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\r\n        unordered_map&lt;int,int>map;\/\/\u4e24\u4e2aint\u5206\u522b\u662f\u5143\u7d20\u548c\u51fa\u73b0\u7684\u6b21\u6570\r\n        for(auto&amp; c:nums){\r\n            map&#91;c]++;\r\n        }\r\n    \/\/2.\u5229\u7528\u4f18\u5148\u961f\u5217\uff0c\u5c06\u51fa\u73b0\u6b21\u6570\u6392\u5e8f\r\n        \/\/\u81ea\u5b9a\u4e49\u4f18\u5148\u961f\u5217\u7684\u6bd4\u8f83\u65b9\u5f0f\uff0c\u5c0f\u9876\u5806\r\n        struct myComparison{\r\n            bool operator()(pair&lt;int,int>&amp;p1,pair&lt;int,int>&amp;p2){\r\n                return p1.second>p2.second;\/\/\u5c0f\u9876\u5806\u662f\u5927\u4e8e\u53f7\r\n            }\r\n        };\r\n        \/\/\u521b\u5efa\u4f18\u5148\u961f\u5217\r\n        priority_queue&lt;pair&lt;int,int>,vector&lt;pair&lt;int,int>>,myComparison> q;\r\n        \/\/\u904d\u5386map\u4e2d\u7684\u5143\u7d20\r\n        \/\/1.\u7ba1\u4ed6\u662f\u5565\uff0c\u5148\u5165\u961f\u5217\uff0c\u961f\u5217\u4f1a\u81ea\u5df1\u6392\u5e8f\u5c06\u4ed6\u653e\u5728\u5408\u9002\u7684\u4f4d\u7f6e\r\n        \/\/2.\u82e5\u961f\u5217\u5143\u7d20\u4e2a\u6570\u8d85\u8fc7k\uff0c\u5219\u5c06\u6808\u9876\u5143\u7d20\u51fa\u6808\uff08\u6808\u9876\u5143\u7d20\u4e00\u5b9a\u662f\u6700\u5c0f\u7684\u90a3\u4e2a\uff09\r\n        for(auto&amp; a:map){\r\n            q.push(a);\r\n            if(q.size()>k){\r\n               q.pop(); \r\n            }\r\n        }\r\n        \/\/\u5c06\u7ed3\u679c\u5bfc\u51fa\r\n        vector&lt;int>res;\r\n        while(!q.empty()){\r\n            res.emplace_back(q.top().first);\r\n            q.pop();\r\n        }\r\n        return res;\r\n\r\n    }\r\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5e76\u4e0d\u96be\uff0c\u9996\u5148\u904d\u5386\u4e00\u6b21\u6570\u7ec4\uff0c\u7528\u54c8\u5e0c\u8868\u8ba1\u6570\uff0c\u7136\u540e\u5bf9\u6b21\u6570\u6392\u5e8f\uff0c\u8fd4\u56de\u524dk\u4e2a\u503c\u5c31\u884c\u3002 \u8bb0\u5f55\u4e00\u4e0bunordered_map\u7684\u7528\u6cd5\u65b9\u4fbf\u4ee5\u540e\u67e5 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12,6,5,11],"tags":[16,15],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-6","category-5","category-11","tag-priority_queue","tag-unorderer_map"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":2,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/258\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}