{"id":1905,"date":"2022-01-12T18:09:25","date_gmt":"2022-01-12T10:09:25","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1905"},"modified":"2022-01-12T18:09:26","modified_gmt":"2022-01-12T10:09:26","slug":"leetcodeday18","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday18\/","title":{"rendered":"leetcodeday18 &#8212;-\u56db\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u7531 n \u4e2a\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4&nbsp;nums \uff0c\u548c\u4e00\u4e2a\u76ee\u6807\u503c target \u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u6ee1\u8db3\u4e0b\u8ff0\u5168\u90e8\u6761\u4ef6\u4e14\u4e0d\u91cd\u590d\u7684\u56db\u5143\u7ec4&nbsp;[nums[a], nums[b], nums[c], nums[d]]&nbsp;\uff08\u82e5\u4e24\u4e2a\u56db\u5143\u7ec4\u5143\u7d20\u4e00\u4e00\u5bf9\u5e94\uff0c\u5219\u8ba4\u4e3a\u4e24\u4e2a\u56db\u5143\u7ec4\u91cd\u590d\uff09\uff1a<\/p>\n\n\n\n<p>0 &lt;= a, b, c, d&nbsp;&lt; n<br>a\u3001b\u3001c \u548c d \u4e92\u4e0d\u76f8\u540c<br>nums[a] + nums[b] + nums[c] + nums[d] == target<br>\u4f60\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u7b54\u6848 \u3002<\/p>\n\n\n\n<p>\u793a\u4f8b 1\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1anums = [1,0,-1,0,-2,2], target = 0<br>\u8f93\u51fa\uff1a[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]<br>\u793a\u4f8b 2\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1anums = [2,2,2,2,2], target = 8<br>\u8f93\u51fa\uff1a[[2,2,2,2]]<\/p>\n\n\n\n<p>\u601d\u8def\uff1a\u4e09\u6570\u4e4b\u548c+\u4e00\u5c42\u904d\u5386<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\nclass Solution:\r\n  def fourSum(self, nums: List&#91;int], target: int) -> List&#91;List&#91;int]]:\r\n    n = len(nums)\r\n    nums.sort()\r\n    result=list()\r\n    def threeSum(nums: List&#91;int],targets: int,fst:int) -> List&#91;List&#91;int]]:\r\n        n = len(nums)\r\n        ans = list()\r\n        \r\n        # \u679a\u4e3e a\r\n        for first in range(n):\r\n            # \u9700\u8981\u548c\u4e0a\u4e00\u6b21\u679a\u4e3e\u7684\u6570\u4e0d\u76f8\u540c\r\n            if first > 0 and nums&#91;first] == nums&#91;first - 1]:\r\n                continue\r\n            # c \u5bf9\u5e94\u7684\u6307\u9488\u521d\u59cb\u6307\u5411\u6570\u7ec4\u7684\u6700\u53f3\u7aef\r\n            third = n - 1\r\n            target = targets-nums&#91;first]\r\n            # \u679a\u4e3e b\r\n            for second in range(first + 1, n):\r\n                # \u9700\u8981\u548c\u4e0a\u4e00\u6b21\u679a\u4e3e\u7684\u6570\u4e0d\u76f8\u540c\r\n                if second > first + 1 and nums&#91;second] == nums&#91;second - 1]:\r\n                    continue\r\n                # \u9700\u8981\u4fdd\u8bc1 b \u7684\u6307\u9488\u5728 c \u7684\u6307\u9488\u7684\u5de6\u4fa7\r\n                while second &lt; third and nums&#91;second] + nums&#91;third] > target:\r\n                    third -= 1\r\n                # \u5982\u679c\u6307\u9488\u91cd\u5408\uff0c\u968f\u7740 b \u540e\u7eed\u7684\u589e\u52a0\r\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\r\n                if second == third:\r\n                    break\r\n                if nums&#91;second] + nums&#91;third] == target:\r\n                    ans.append(&#91;fst,nums&#91;first], nums&#91;second], nums&#91;third]])\r\n        \r\n        return ans\r\n    \r\n    for first in range(n):\r\n        if first > 0 and nums&#91;first] == nums&#91;first - 1]:\r\n                continue\r\n        targets= target-nums&#91;first]\r\n        result.extend(threeSum(nums&#91;first+1:],targets,nums&#91;first]))\r\n    return result<\/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-199.png\" alt=\"\" class=\"wp-image-1909\" width=\"524\" height=\"152\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-199.png 658w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-199-300x88.png 300w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u7531 n \u4e2a\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4&nbsp;nums \uff0c\u548c\u4e00\u4e2a\u76ee\u6807\u503c target \u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u6ee1\u8db3\u4e0b\u8ff0\u5168 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday18\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday18 &#8212;-\u56db\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\/1905"}],"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=1905"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"predecessor-version":[{"id":1910,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1905\/revisions\/1910"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}