{"id":31,"date":"2024-09-30T14:44:28","date_gmt":"2024-09-30T06:44:28","guid":{"rendered":"https:\/\/blog.zeronely.cn\/?p=31"},"modified":"2024-09-30T14:44:29","modified_gmt":"2024-09-30T06:44:29","slug":"%e5%88%b7%e9%a2%98%e5%91%a8%e8%ae%b0%ef%bc%882%ef%bc%89-128-%e6%9c%80%e9%95%bf%e8%bf%9e%e7%bb%ad%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/blog.zeronely.cn\/index.php\/2024\/09\/30\/%e5%88%b7%e9%a2%98%e5%91%a8%e8%ae%b0%ef%bc%882%ef%bc%89-128-%e6%9c%80%e9%95%bf%e8%bf%9e%e7%bb%ad%e5%ba%8f%e5%88%97\/","title":{"rendered":"\u5237\u9898\u5468\u8bb0\uff082\uff09\u2014\u2014128.\u6700\u957f\u8fde\u7eed\u5e8f\u5217"},"content":{"rendered":"\n<p>\u4e0a\u5468\u5237\u5b8c\u4e86leetcode\u201c\u70ed\u9898100\u201d\u9898\u5355\u4e2d\u7684\u6240\u6709\u7b80\u5355\u9898\uff0c\u5b8c\u6210\u4e86\u5bf9\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u57fa\u7840\u590d\u4e60\uff0c\u63a5\u4e0b\u6765\u9884\u8ba1\u6309\u7167\u6bcf\u5929\u81f3\u5c111\u9053\u9898\u7684\u8282\u594f\uff0c\u5bf9leetcode\u201c\u70ed\u9898100\u201d\u9898\u5355\u4e2d\u7684\u9898\u76ee\u8fdb\u884c\u5b8c\u6210\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">128.\u6700\u957f\u8fde\u7eed\u5e8f\u5217<\/h2>\n\n\n\n<p>\u9898\u76ee\u8981\u6c42\u4ece\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u6570\u7ec4\u4e2d\uff0c\u627e\u5230\u4e00\u4e2a\u6700\u957f\u7684\u8fde\u7eed\u5e8f\u5217\uff0c\u81ea\u7136\u7684\u60f3\u6cd5\u662f\uff0c\u4ee5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e3a\u8d77\u59cb\uff0c\u5206\u522b\u5224\u65ad\u4ee5\u5176\u4e3a\u8d77\u59cb\u7684\u6700\u957f\u5e8f\u5217\u7684\u957f\u5ea6\u3002\u4e8e\u662f\uff0c\u5199\u51fa\u4e86\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int longestConsecutive(vector&lt;int>&amp; nums) \n    {\n        unordered_map&lt;int,int> mp;\n        for(int i=0;i&lt;nums.size();i++)\n        {\n            mp&#91;nums&#91;i]]=nums&#91;i];\n        }\n        int ans=0;\n        for(int i=0;i&lt;nums.size();i++)\n        {\n            int temp_ans=1;\n            int temp=nums&#91;i];\n            while(mp.count(++temp))\n            {\n                temp_ans++;\n            }\n            if(temp_ans>ans) ans=temp_ans;\n        }\n        return ans;\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u663e\u7136\uff0c\u8fd9\u662f\u4e00\u79cd\u5f88\u66b4\u529b\u7684\u65b9\u6cd5<s>\uff08\u5f53\u7136\uff0c\u4e0d\u662f\u6700\u66b4\u529b\uff09<\/s>\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8fbe\u5230\u4e86O\uff08n<sup>2<\/sup>\uff09\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u8fbe\u5230O\uff08n\uff09\uff0c\u8fd9\u91cc\u8fd0\u7528\u4e86\u54c8\u5e0c\u8868\uff0c\u5c06\u67e5\u627e\u201c\u4e0b\u4e00\u4e2a\u5143\u7d20\u201d\u7684\u590d\u6742\u5ea6\u964d\u4f4e\u4e86\u3002\u7136\u800c\u9898\u76ee\u8981\u6c42\u7684\u590d\u6742\u5ea6\u662fO\uff08n\uff09\uff0c\u663e\u7136\uff0c\u8d85\u65f6\u4e86\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\uff0c\u901a\u8fc7\u9605\u8bfb\u5b98\u65b9\u9898\u89e3\uff0c\u627e\u5230\u4e86\u4f18\u5316\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u7684\u524d\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u5b58\u5728\uff0c\u82e5\u5b58\u5728\uff0c\u8bc1\u660e\u5176\u4e00\u5b9a\u4e0d\u662f\u6700\u957f\u5e8f\u5217\u7684\u5f00\u5934\uff0c\u4e8e\u662f\u8df3\u8fc7\u3002\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u590d\u6742\u5ea6\u964d\u4f4e\u5230O\uff08n\uff09\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int longestConsecutive(vector&lt;int>&amp; nums) \n    {\n        unordered_map&lt;int,int> mp;\n        for(int i=0;i&lt;nums.size();i++)\n        {\n            mp&#91;nums&#91;i]]=nums&#91;i];\n        }\n        int ans=0;\n        for(int i=0;i&lt;nums.size();i++)\n        {\n            int temp_ans=1;\n            int temp=nums&#91;i];\n            if(mp.count(temp-1)) continue;\/\/\u8df3\u8fc7\u975e\u5e8f\u5217\u5934\u7684\u5143\u7d20\n            while(mp.count(++temp))\n            {\n                temp_ans++;\n            }\n            if(temp_ans>ans) ans=temp_ans;\n        }\n        return ans;\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\uff0c\u8fd8\u6709\u4f18\u5316\u7a7a\u95f4\uff0c\u6b64\u5904\uff0c\u66f4\u5408\u9002\u7684\u662f\u4f7f\u7528unordered_set\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u5468\u5237\u5b8c\u4e86leetcode\u201c\u70ed\u9898100\u201d\u9898\u5355\u4e2d\u7684\u6240\u6709\u7b80\u5355\u9898\uff0c\u5b8c\u6210\u4e86\u5bf9\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u57fa\u7840\u590d\u4e60\uff0c\u63a5\u4e0b\u6765\u9884\u8ba1\u6309\u7167\u6bcf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,3],"tags":[9],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-3","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":3,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/posts\/31\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zeronely.cn\/index.php\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}