{"id":1828,"date":"2022-01-12T10:26:38","date_gmt":"2022-01-12T02:26:38","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1828"},"modified":"2022-01-12T10:26:39","modified_gmt":"2022-01-12T02:26:39","slug":"leetcodeday15","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday15\/","title":{"rendered":"leetcodeday15 &#8212;\u4e09\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5305\u542b&nbsp;<code>n<\/code>&nbsp;\u4e2a\u6574\u6570\u7684\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u5224\u65ad&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u662f\u5426\u5b58\u5728\u4e09\u4e2a\u5143\u7d20&nbsp;<em>a\uff0cb\uff0cc \uff0c<\/em>\u4f7f\u5f97&nbsp;<em>a + b + c =&nbsp;<\/em>0 \uff1f\u8bf7\u4f60\u627e\u51fa\u6240\u6709\u548c\u4e3a&nbsp;<code>0<\/code>&nbsp;\u4e14\u4e0d\u91cd\u590d\u7684\u4e09\u5143\u7ec4\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u7b54\u6848\u4e2d\u4e0d\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u7684\u4e09\u5143\u7ec4\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;-1,0,1,2,-1,-4]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;-1,-1,2],&#91;-1,0,1]]<\/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;]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>nums = &#91;0]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/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;= 3000<\/code><\/li><li><code>-10<sup>5<\/sup> &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>\u601d\u8def\u53cc\u6307\u9488\u65b9\u6cd5:\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5982\u4e0b\u5224\u65ad\uff1a-s[i]==s[j]+s[m]\uff0c\u5176\u4e2d s[j]+s[m] \u8981\u904d\u5386\u5269\u4e0b\u7684\u5143\u7d20\uff08\u91c7\u7528\u53cc\u6307\u9488\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# @lc code=start\nclass Solution:\n    def threeSum(self, nums: List&#91;int]) -&gt; List&#91;List&#91;int]]:\n            if set(nums)=={0} and len(nums)&gt;=3:\n                return &#91;&#91;0,0,0]]\n                   \n            sets=set()\n            lens=len(nums)\n            nums =sorted(nums)\n            result=&#91;]\n            for i in range(lens):\n                newlist=nums&#91;0:i]+nums&#91;i+1:]\n                \n                m=0\n                j=lens-1-1\n                if nums&#91;i] in sets:\n                    continue\n                else:\n                    sets.add(nums&#91;i])\n                while m&lt;j:\n                    if newlist&#91;m]+newlist&#91;j]==-nums&#91;i] and sorted(&#91;newlist&#91;m],newlist&#91;j],nums&#91;i]])not  in result:\n            \n                        result.append(sorted(&#91;newlist&#91;m],newlist&#91;j],nums&#91;i]]))\n                        m=m+1\n                    elif newlist&#91;m]+newlist&#91;j]&gt;-nums&#91;i]:\n                        j=j-1\n                    else:\n                        m=m+1\n            return result\n\n# @lc code=end<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-184.png\" alt=\"\" class=\"wp-image-1834\" width=\"452\" height=\"181\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-184.png 555w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-184-300x120.png 300w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/figure><\/div>\n\n\n\n<p>\u5c31\u5dee\u8d85\u65f6\u4e86&#8230;&#8230;<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a\u6392\u5e8f + \u53cc\u6307\u9488<br>\u9898\u76ee\u4e2d\u8981\u6c42\u627e\u5230\u6240\u6709\u300c\u4e0d\u91cd\u590d\u300d\u4e14\u548c\u4e3a 0 \u7684\u4e09\u5143\u7ec4\uff0c\u8fd9\u4e2a\u300c\u4e0d\u91cd\u590d\u300d\u7684\u8981\u6c42\u4f7f\u5f97\u6211\u4eec\u65e0\u6cd5\u7b80\u5355\u5730\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u679a\u4e3e\u6240\u6709\u7684\u4e09\u5143\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5168\u90e8\u4e3a 0\uff0c\u5373<\/p>\n\n\n\n<p>[0, 0, 0, 0, 0, \u2026, 0, 0, 0]<br>\u4efb\u610f\u4e00\u4e2a\u4e09\u5143\u7ec4\u7684\u548c\u90fd\u4e3a 00\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u679a\u4e3e\u4e09\u5143\u7ec4\uff0c\u4f1a\u5f97\u5230 O(N^3)\u4e2a\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u4e09\u5143\u7ec4\uff08\u5176\u4e2d N\u662f\u6570\u7ec4\u7684\u957f\u5ea6\uff09\u65f6\u95f4\u590d\u6742\u5ea6\u81f3\u5c11\u4e3a O(N^3)\u3002\u5728\u8fd9\u4e4b\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4f7f\u7528\u54c8\u5e0c\u8868\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u5f97\u5230\u4e0d\u5305\u542b\u91cd\u590d\u4e09\u5143\u7ec4\u7684\u6700\u7ec8\u7b54\u6848\uff0c\u53c8\u6d88\u8017\u4e86\u5927\u91cf\u7684\u7a7a\u95f4\u3002\u8fd9\u4e2a\u505a\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u5f88\u9ad8\uff0c\u56e0\u6b64\u6211\u4eec\u8981\u6362\u4e00\u79cd\u601d\u8def\u6765\u8003\u8651\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u300c\u4e0d\u91cd\u590d\u300d\u7684\u672c\u8d28\u662f\u4ec0\u4e48\uff1f\u6211\u4eec\u4fdd\u6301\u4e09\u91cd\u5faa\u73af\u7684\u5927\u6846\u67b6\u4e0d\u53d8\uff0c\u53ea\u9700\u8981\u4fdd\u8bc1\uff1a<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u91cd\u5faa\u73af\u679a\u4e3e\u5230\u7684\u5143\u7d20\u4e0d\u5c0f\u4e8e\u5f53\u524d\u7b2c\u4e00\u91cd\u5faa\u73af\u679a\u4e3e\u5230\u7684\u5143\u7d20\uff1b<\/p>\n\n\n\n<p>\u7b2c\u4e09\u91cd\u5faa\u73af\u679a\u4e3e\u5230\u7684\u5143\u7d20\u4e0d\u5c0f\u4e8e\u5f53\u524d\u7b2c\u4e8c\u91cd\u5faa\u73af\u679a\u4e3e\u5230\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u679a\u4e3e\u7684\u4e09\u5143\u7ec4 (a, b, c) \u6ee1\u8db3 a\u2264b\u2264c\uff0c\u4fdd\u8bc1\u4e86\u53ea\u6709 (a,b,c) \u8fd9\u4e2a\u987a\u5e8f\u4f1a\u88ab\u679a\u4e3e\u5230\uff0c\u800c (b,a,c)\u3001(c,b,a) \u7b49\u7b49\u8fd9\u4e9b\u4e0d\u4f1a\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u91cd\u590d\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u8fdb\u884c\u6392\u5e8f\uff0c\u968f\u540e\u4f7f\u7528\u666e\u901a\u7684\u4e09\u91cd\u5faa\u73af\u5c31\u53ef\u4ee5\u6ee1\u8db3\u4e0a\u9762\u7684\u8981\u6c42\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u91cd\u5faa\u73af\u800c\u8a00\uff0c\u76f8\u90bb\u4e24\u6b21\u679a\u4e3e\u7684\u5143\u7d20\u4e0d\u80fd\u76f8\u540c\uff0c\u5426\u5219\u4e5f\u4f1a\u9020\u6210\u91cd\u590d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6392\u5b8c\u5e8f\u7684\u6570\u7ec4\u4e3a<\/p>\n\n\n\n<p>[0, 1, 2, 2, 2, 3]<br><br>\u6211\u4eec\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u679a\u4e3e\u5230\u7684\u7b2c\u4e00\u4e2a\u4e09\u5143\u7ec4\u4e3a (0, 1, 2)\uff0c\u5982\u679c\u7b2c\u4e09\u91cd\u5faa\u73af\u7ee7\u7eed\u679a\u4e3e\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u4ecd\u7136\u662f\u4e09\u5143\u7ec4 (0, 1, 2)\uff0c\u4ea7\u751f\u4e86\u91cd\u590d\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u7b2c\u4e09\u91cd\u5faa\u73af\u300c\u8df3\u5230\u300d\u4e0b\u4e00\u4e2a\u4e0d\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5373\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20 3\uff0c\u679a\u4e3e\u4e09\u5143\u7ec4 (0, 1, 3)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums.sort()\nfor first = 0 .. n-1\n    \/\/ \u53ea\u6709\u548c\u4e0a\u4e00\u6b21\u679a\u4e3e\u7684\u5143\u7d20\u4e0d\u76f8\u540c\uff0c\u6211\u4eec\u624d\u4f1a\u8fdb\u884c\u679a\u4e3e\n    if first == 0 or nums&#91;first] != nums&#91;first-1] then\n        for second = first+1 .. n-1\n            if second == first+1 or nums&#91;second] != nums&#91;second-1] then\n                for third = second+1 .. n-1\n                    if third == second+1 or nums&#91;third] != nums&#91;third-1] then\n                        \/\/ \u5224\u65ad\u662f\u5426\u6709 a+b+c==0\n                        check(first, second, third)\n\n\u4f5c\u8005\uff1aLeetCode-Solution\n\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/3sum\/solution\/san-shu-zhi-he-by-leetcode-solution\/\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/code><\/pre>\n\n\n\n<p>\u6539\u8fdb\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"773\" height=\"207\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-189.png\" alt=\"\" class=\"wp-image-1845\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-189.png 773w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-189-300x80.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-189-768x206.png 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>nums.sort()\nfor first = 0 .. n-1\n    if first == 0 or nums&#91;first] != nums&#91;first-1] then\n        \/\/ \u7b2c\u4e09\u91cd\u5faa\u73af\u5bf9\u5e94\u7684\u6307\u9488\n        third = n-1\n        for second = first+1 .. n-1\n            if second == first+1 or nums&#91;second] != nums&#91;second-1] then\n                \/\/ \u5411\u5de6\u79fb\u52a8\u6307\u9488\uff0c\u76f4\u5230 a+b+c \u4e0d\u5927\u4e8e 0\n                while nums&#91;first]+nums&#91;second]+nums&#91;third] > 0\n                    third = third-1\n                \/\/ \u5224\u65ad\u662f\u5426\u6709 a+b+c==0\n                check(first, second, third)\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"782\" height=\"267\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-190.png\" alt=\"\" class=\"wp-image-1847\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-190.png 782w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-190-300x102.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-190-768x262.png 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def threeSum(self, nums: List&#91;int]) -> List&#91;List&#91;int]]:\n        n = len(nums)\n        nums.sort()\n        ans = list()\n        \n        # \u679a\u4e3e a\n        for first in range(n):\n            # \u9700\u8981\u548c\u4e0a\u4e00\u6b21\u679a\u4e3e\u7684\u6570\u4e0d\u76f8\u540c\n            if first > 0 and nums&#91;first] == nums&#91;first - 1]:\n                continue\n            # c \u5bf9\u5e94\u7684\u6307\u9488\u521d\u59cb\u6307\u5411\u6570\u7ec4\u7684\u6700\u53f3\u7aef\n            third = n - 1\n            target = -nums&#91;first]\n            # \u679a\u4e3e b\n            for second in range(first + 1, n):\n                # \u9700\u8981\u548c\u4e0a\u4e00\u6b21\u679a\u4e3e\u7684\u6570\u4e0d\u76f8\u540c\n                if second > first + 1 and nums&#91;second] == nums&#91;second - 1]:\n                    continue\n                # \u9700\u8981\u4fdd\u8bc1 b \u7684\u6307\u9488\u5728 c \u7684\u6307\u9488\u7684\u5de6\u4fa7\n                while second &lt; third and nums&#91;second] + nums&#91;third] > target:\n                    third -= 1\n                # \u5982\u679c\u6307\u9488\u91cd\u5408\uff0c\u968f\u7740 b \u540e\u7eed\u7684\u589e\u52a0\n                # \u5c31\u4e0d\u4f1a\u6709\u6ee1\u8db3 a+b+c=0 \u5e76\u4e14 b&lt;c \u7684 c \u4e86\uff0c\u53ef\u4ee5\u9000\u51fa\u5faa\u73af\n                if second == third:\n                    break\n                if nums&#91;second] + nums&#91;third] == target:\n                    ans.append(&#91;nums&#91;first], nums&#91;second], nums&#91;third]])\n        \n        return ans\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5305\u542b&nbsp;n&nbsp;\u4e2a\u6574\u6570\u7684\u6570\u7ec4&nbsp;nums\uff0c\u5224\u65ad&nbsp;nums&nbsp;\u4e2d &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday15\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday15 &#8212;\u4e09\u6570\u4e4b\u548c<\/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\/1828"}],"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=1828"}],"version-history":[{"count":9,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1828\/revisions"}],"predecessor-version":[{"id":1848,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1828\/revisions\/1848"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1828"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}