{"id":155,"date":"2021-10-31T01:32:34","date_gmt":"2021-10-30T17:32:34","guid":{"rendered":"http:\/\/139.224.63.49\/?p=155"},"modified":"2021-10-31T01:32:50","modified_gmt":"2021-10-30T17:32:50","slug":"c%e9%81%8d%e5%8e%86map%e6%97%b6%e8%a6%81%e6%b3%a8%e6%84%8f%e7%9a%84%e4%b8%80%e4%b8%aa%e5%b0%8f%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=155","title":{"rendered":"[c++]\u904d\u5386map\u65f6\u8981\u6ce8\u610f\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898"},"content":{"rendered":"\n<p>\u6211\u4eec\u90fd\u77e5\u9053\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5bb9\u5668\u7684\u65f6\u5019\uff0c\u5982\u679c\u5bf9\u5bb9\u5668\u8fdb\u884c\u63d2\u5165\u6216\u8005\u5220\u9664\u64cd\u4f5c\uff0c\u4f1a\u9020\u6210\u8fed\u4ee3\u5668\u5931\u6548\uff0c\u4ece\u800c\u65e0\u6cd5\u6b63\u5e38\u904d\u5386\u3002\u4f46\u6709\u65f6\u5019\u5bf9\u5bb9\u5668\u7684\u63d2\u5165\u6216\u8005\u5220\u9664\u64cd\u4f5c\u4e0d\u662f\u90a3\u4e48\u663e\u800c\u6613\u89c1\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u9898\u76ee\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u7684\u6570\u5b57k\uff0c\u5982\u679c\u6570\u7ec4\u4e2d\u5b58\u5728k+1\u51fa\u73b0\u7684\u6b21\u6570\u6bd4k\u51fa\u73b0\u7684\u6b21\u6570\u591a\uff0c\u5219\u8f93\u51fak<\/p>\n\n\n\n<p>\u5982\u679c\u5199\u4e0b\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nvector&lt;int&gt; work(const vector&lt;int&gt;&amp; nums)\n{\n    map&lt;int,int&gt; counter;\n    for(const int&amp; x:nums)\n    {\n        ++counter&#91;x];\n    }\n    vector&lt;int&gt; ret;\n    for(const auto&amp;&#91;k,v]:counter)\n    {\n        if(counter&#91;k+1]&gt;counter&#91;k])\n            ret.emplace_back(k);\n    }\n    return ret;\n}\nint main()\n{\n    vector&lt;int&gt; nums{1,1,2,2,2,3,3,3,3};\n    vector&lt;int&gt; ret = work(nums);\n    for(const int&amp; x:ret)\n    {\n        cout&lt;&lt;x&lt;&lt;\" \";\n    }\n    cout&lt;&lt;endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5c31\u51fa\u9519\u4e86\u3002\u3002\u3002<\/p>\n\n\n\n<p>\u5728\u6211\u7684\u6d4b\u8bd5\u73af\u5883\u4e0b\uff0cdebian\uff0cg++\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u76f4\u8fd0\u884c\uff0c\u7c7b\u4f3c\u6b7b\u5faa\u73af\u4e86\u3002\u3002<\/p>\n\n\n\n<p>\u95ee\u9898\u5728\u54ea\u5462\uff1f\u95ee\u9898\u5728\u4e8e\u5728\u904d\u5386counter\u8fd9\u4e2amap\u7684\u65f6\u5019\uff0cfor\u5faa\u73af\u4e2d\u6709\u4e00\u53e5\uff1a if(counter[k+1]&gt;counter[k]) <\/p>\n\n\n\n<p>\u8fd9\u4e00\u53e5\u8bdd\u4e2d counter[k+1] \u8fdb\u884c\u4e86\u4ec0\u4e48\u64cd\u4f5c\u5462\uff1f\u5982\u679ccounter\u4e2d\u6709k+1\u8fd9\u4e2akey\uff0c\u5c31\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684value\u3002\u4f46\u662f\u5982\u679c\u4e0d\u5b58\u5728k+1\u8fd9\u4e2akey\uff0c\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2akey=k+1,\u5e76\u5bf9\u5bf9\u5e94\u7684value\u6267\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u5982\u679ccounter\u4e2d\u6ca1\u6709k+1\u8fd9\u4e2akey\uff0c\u6267\u884ccounter[k+1]\u5c31\u4f1a\u5bf9counter\u8fdb\u884c\u63d2\u5165\u7684\u64cd\u4f5c\u3002\u76f8\u5f53\u4e8ecounter.emplace(k+1,0)<\/p>\n\n\n\n<p>\u56e0\u4e3a\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u5bf9\u5bb9\u5668\u8fdb\u884c\u4e86\u63d2\u5165\u64cd\u4f5c\uff0c\u9020\u6210\u4e86\u8fed\u4ee3\u5668\u5931\u6548\uff0c\u6240\u4ee5\u8fd9\u4e2a\u904d\u5386\u7684\u5faa\u73af\u65e0\u6cd5\u6b63\u5e38\u7ec8\u6b62\uff0c\u5c31\u4e00\u76f4\u5faa\u73af\u4e86\u3002\u3002\u4f30\u8ba1\u4f1a\u76f4\u5230\u78b0\u5230\u65e0\u6cd5\u8bbf\u95ee\u7684\u5185\u5b58\u624d\u4f1a\u62a5\u9519\u7136\u540e\u9000\u51fa\u3002<\/p>\n\n\n\n<p>\u5c06work\u51fd\u6570\u6539\u4e0b\u5373\u53ef\u6b63\u5e38\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vector&lt;int&gt; work(const vector&lt;int&gt;&amp; nums)\n{\n    map&lt;int,int&gt; counter;\n    for(const int&amp; x:nums)\n    {\n        ++counter&#91;x];\n    }\n    vector&lt;int&gt; ret;\n    for(const auto&amp;&#91;k,v]:counter)\n    {\n        auto it = counter.find(k+1);\n        if(it!=counter.end()&amp;&amp;it-&gt;second&gt;counter&#91;k])\n            ret.emplace_back(k);\n    }\n    return ret;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5bb9\u5668\u7684\u65f6\u5019\uff0c\u5982\u679c\u5bf9\u5bb9\u5668\u8fdb\u884c\u63d2\u5165\u6216\u8005\u5220\u9664\u64cd\u4f5c\uff0c\u4f1a\u9020\u6210\u8fed\u4ee3\u5668\u5931\u6548\uff0c\u4ece\u800c\u65e0\u6cd5\u6b63\u5e38\u904d\u5386&hellip; <a href=\"http:\/\/iamnear.top\/?p=155\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">[c++]\u904d\u5386map\u65f6\u8981\u6ce8\u610f\u7684\u4e00\u4e2a\u5c0f\u95ee\u9898<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":{"0":"post-155","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-c_plus_plus","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":2,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":157,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions\/157"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}