{"id":210,"date":"2024-11-28T20:41:09","date_gmt":"2024-11-28T12:41:09","guid":{"rendered":"http:\/\/getzs.online\/?p=210"},"modified":"2024-11-28T20:41:09","modified_gmt":"2024-11-28T12:41:09","slug":"leetcode-hot100-4","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2024\/11\/28\/leetcode-hot100-4\/","title":{"rendered":"LeetCode Hot100 -4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/longest-consecutive-sequence\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">128. \u6700\u957f\u8fde\u7eed\u5e8f\u5217<\/a><\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u627e\u51fa\u6570\u5b57\u8fde\u7eed\u7684\u6700\u957f\u5e8f\u5217\uff08\u4e0d\u8981\u6c42\u5e8f\u5217\u5143\u7d20\u5728\u539f\u6570\u7ec4\u4e2d\u8fde\u7eed\uff09\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(n)<\/code><em>&nbsp;<\/em>\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [100,4,200,1,3,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u957f\u6570\u5b57\u8fde\u7eed\u5e8f\u5217\u662f [1, 2, 3, 4]\u3002\u5b83\u7684\u957f\u5ea6\u4e3a 4\u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [0,3,7,2,5,8,4,6,0,1]<br><strong>\u8f93\u51fa\uff1a<\/strong>9<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>0 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c4\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>        \u5229\u7528\u6392\u5e8f+\u6ed1\u52a8\u7a97\u53e3\u7684\u505a\u6cd5\uff0c\u5c06\u6570\u7ec4\u6392\u5e8f\u540e\uff0c\u901a\u8fc7\u6bd4\u8f83\u76f8\u90bb\u5dee\u503c\uff0c\u76f8\u540c\u5219\u53bb\u91cd\uff08\u8868\u793a\u4e3ai++\uff09\uff0c\u5dee1\u7b26\u5408\u6761\u4ef6\uff0c\u53cd\u4e4b\u5c06\u5de6\u6307\u9488\u91cd\u7f6e\uff0c\u5373\u53ef\u627e\u5230\u6700\u5927\u8fde\u7eed\u6570\u7ec4\u957f\u5ea6\u3002\u590d\u6742\u5ea6O(nlog(n))\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic int longestConsecutive(int&#91;] nums) {\n\t\tint n = nums.length;\n\t\tArrays.sort(nums);\n\t\tint len = Math.min(1, n);\n\t\tint i = 0;\n\t\tfor (int j = 1 ; j &lt; n ; j++) {\n\t\t\tif (nums&#91;j] == nums&#91;j - 1]) {\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (nums&#91;j] - 1 != nums&#91;j - 1]) {\n\t\t\t\ti = j;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tlen = Math.max(len, j - i + 1);\n\t\t\t}\n\t\t}\n\t\treturn len;\n\t}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fdb\u9636\u505a\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u5229\u7528set\u53bb\u91cd\uff0c\u7136\u540e\u901a\u8fc7\u679a\u4e3e\u7684\u5f62\u5f0f\uff0c\u5224\u65ad\u6700\u957f\u8fde\u7eed\u4e2a\u6570\uff0c\u590d\u6742\u5ea6O(n)\u3002<\/p>\n\n\n\n<p>\u6ce8\uff1a\u5224\u65adx-1\u662f\u5426\u51fa\u73b0\uff0c\u53bb\u6389\u91cd\u590d\u679a\u4e3e\uff0c\u53ea\u4ece\u6bcf\u4e2a\u8fde\u7eed\u6570\u7ec4\u5f00\u59cb\u7684\u7b2c\u4e00\u4f4d\u679a\u4e3e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic int longestConsecutive(int&#91;] nums) {\n\t\tSet&lt;Integer> set = new HashSet&lt;>();\n\t\tfor (int num : nums) {\n\t\t\tset.add(num);\n\t\t}\n\t\tint len = 0;\n\t\tfor (int x : set) {\n\t\t\tif (set.contains(x - 1)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint y = x + 1;\n\t\t\twhile (set.contains(y)) {\n\t\t\t\ty++;\n\t\t\t}\n\t\t\tlen = Math.max(len, y - x);\n\t\t}\n\t\treturn len;\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>128. \u6700\u957f\u8fde\u7eed\u5e8f\u5217 \u7ed9\u5b9a\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\uff0c\u627e\u51fa\u6570\u5b57\u8fde\u7eed\u7684\u6700\u957f\u5e8f\u5217\uff08\u4e0d\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-leetcode-hot100"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}