{"id":2606,"date":"2022-02-04T14:34:27","date_gmt":"2022-02-04T06:34:27","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2606"},"modified":"2022-02-04T14:34:28","modified_gmt":"2022-02-04T06:34:28","slug":"leetcodeday128","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday128\/","title":{"rendered":"leetcodeday128&#8211;\u6700\u957f\u8fde\u7eed\u5e8f\u5217"},"content":{"rendered":"\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>\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-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>nums = &#91;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 &#91;1, 2, 3, 4]\u3002\u5b83\u7684\u957f\u5ea6\u4e3a 4\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>nums = &#91;0,3,7,2,5,8,4,6,0,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>9<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>0 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup> &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=128 lang=python3\r\n#\r\n# &#91;128] \u6700\u957f\u8fde\u7eed\u5e8f\u5217\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def longestConsecutive(self, nums: List&#91;int]) -> int:\r\n        if nums==&#91;]:\r\n            return 0\r\n        nums.sort()\r\n        m=1\r\n        x=1\r\n        for i in range(len(nums)-1):\r\n            if nums&#91;i]==nums&#91;i+1]:\r\n                continue\r\n            if nums&#91;i]==nums&#91;i+1]-1:\r\n                x=x+1          \r\n                m=max(m,x)\r\n            else:\r\n                x=1\r\n        return m\r\n\r\n# @lc code=end\r\n\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"776\" height=\"250\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-36.png\" alt=\"\" class=\"wp-image-2607\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-36.png 776w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-36-300x97.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-36-768x247.png 768w\" sizes=\"(max-width: 776px) 100vw, 776px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"584\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-38-1024x584.png\" alt=\"\" class=\"wp-image-2611\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-38-1024x584.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-38-300x171.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-38-768x438.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-38.png 1250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\r\n    def longestConsecutive(self, nums: List&#91;int]) -> int:\r\n        longest_streak = 0\r\n        num_set = set(nums)\r\n\r\n        for num in num_set:\r\n            if num - 1 not in num_set:\r\n                current_num = num\r\n                current_streak = 1\r\n\r\n                while current_num + 1 in num_set:\r\n                    current_num += 1\r\n                    current_streak += 1\r\n\r\n                longest_streak = max(longest_streak, current_streak)\r\n\r\n        return longest_streak<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"776\" height=\"286\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-37.png\" alt=\"\" class=\"wp-image-2609\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-37.png 776w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-37-300x111.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-37-768x283.png 768w\" sizes=\"(max-width: 776px) 100vw, 776px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\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\u6c42\u5e8f\u5217\u5143\u7d20\u5728\u539f\u6570\u7ec4\u4e2d\u8fde\u7eed &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday128\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday128&#8211;\u6700\u957f\u8fde\u7eed\u5e8f\u5217<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2606"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2606"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2606\/revisions"}],"predecessor-version":[{"id":2613,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2606\/revisions\/2613"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2606"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}