{"id":2128,"date":"2022-01-19T15:11:15","date_gmt":"2022-01-19T07:11:15","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2128"},"modified":"2022-01-19T15:11:16","modified_gmt":"2022-01-19T07:11:16","slug":"leetcodeday39","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/19\/leetcodeday39\/","title":{"rendered":"leetcodeday39- \u6570\u7ec4\u603b\u548c"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;<strong>\u65e0\u91cd\u590d\u5143\u7d20<\/strong>&nbsp;\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>candidates<\/code>&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570&nbsp;<code>target<\/code>&nbsp;\uff0c\u627e\u51fa&nbsp;<code>candidates<\/code>&nbsp;\u4e2d\u53ef\u4ee5\u4f7f\u6570\u5b57\u548c\u4e3a\u76ee\u6807\u6570&nbsp;<code>target<\/code>&nbsp;\u7684&nbsp;<strong>\u6240\u6709\u4e0d\u540c\u7ec4\u5408<\/strong>&nbsp;\uff0c\u5e76\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u3002\u4f60\u53ef\u4ee5\u6309&nbsp;<strong>\u4efb\u610f\u987a\u5e8f<\/strong>&nbsp;\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\u3002<\/p>\n\n\n\n<p><code>candidates<\/code>&nbsp;\u4e2d\u7684&nbsp;<strong>\u540c\u4e00\u4e2a<\/strong>&nbsp;\u6570\u5b57\u53ef\u4ee5&nbsp;<strong>\u65e0\u9650\u5236\u91cd\u590d\u88ab\u9009\u53d6<\/strong>&nbsp;\u3002\u5982\u679c\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57\u7684\u88ab\u9009\u6570\u91cf\u4e0d\u540c\uff0c\u5219\u4e24\u79cd\u7ec4\u5408\u662f\u4e0d\u540c\u7684\u3002&nbsp;<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u8f93\u5165\uff0c\u4fdd\u8bc1\u548c\u4e3a&nbsp;<code>target<\/code>&nbsp;\u7684\u4e0d\u540c\u7ec4\u5408\u6570\u5c11\u4e8e&nbsp;<code>150<\/code>&nbsp;\u4e2a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>candidates = <code>[2,3,6,7], <\/code>target = <code>7<\/code>\n<strong>\u8f93\u51fa\uff1a<\/strong>[[2,2,3],[7]]\n<strong>\u89e3\u91ca\uff1a<\/strong>\n2 \u548c 3 \u53ef\u4ee5\u5f62\u6210\u4e00\u7ec4\u5019\u9009\uff0c2 + 2 + 3 = 7 \u3002\u6ce8\u610f 2 \u53ef\u4ee5\u4f7f\u7528\u591a\u6b21\u3002\n7 \u4e5f\u662f\u4e00\u4e2a\u5019\u9009\uff0c 7 = 7 \u3002\n\u4ec5\u6709\u8fd9\u4e24\u79cd\u7ec4\u5408\u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165: <\/strong>candidates = [2,3,5]<code>, <\/code>target = 8\n<strong>\u8f93\u51fa: <\/strong>[[2,2,2,2],[2,3,3],[3,5]]<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165: <\/strong>candidates = <code>[2], <\/code>target = 1\n<strong>\u8f93\u51fa: <\/strong>[]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165: <\/strong>candidates = <code>[1], <\/code>target = <code>1<\/code>\n<strong>\u8f93\u51fa: <\/strong>[[1]]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165: <\/strong>candidates = <code>[1], <\/code>target = <code>2<\/code>\n<strong>\u8f93\u51fa: <\/strong>[[1,1]]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>1 &lt;= candidates.length &lt;= 30<\/code><\/li><li><code>1 &lt;= candidates[i] &lt;= 200<\/code><\/li><li><code>candidate<\/code>&nbsp;\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd&nbsp;<strong>\u4e92\u4e0d\u76f8\u540c<\/strong><\/li><li><code>1 &lt;= target &lt;= 500<\/code><\/li><\/ul>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u521d\u6b21\u5c1d\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=39 lang=python3\n#\n# &#91;39] \u7ec4\u5408\u603b\u548c\n#\n\n# @lc code=start\nclass Solution:\n    def combinationSum(self,candidates, target: int):\n        nums=list()\n        nums.append(-1)\n        length=len(candidates)\n        rev=&#91;target-i for i in candidates]\n        \n        nums.extend(&#91;target-i for i in candidates])\n        while  &#91;i for i in rev if i&gt;=min(candidates)]!=&#91;]:\n            \n            mid=list()\n            for i in rev:\n                if i&lt;=0:\n                    mid.extend(&#91;-1]*length)\n                    nums.extend(&#91;-1]*length)\n                    continue\n                else:\n                    mid.extend(&#91;i-j for j in candidates])\n                    nums.extend(&#91;i-j for j in candidates])\n            rev=mid\n        result=list()\n        mids=list()\n\n        for i in range(1,len(nums)):\n            if nums&#91;i]!=0:\n                continue\n            else:\n              j=i\n              while 1:\n               \n                if j&lt;=length:\n                        mids.append(candidates&#91;j-1])\n                        mids.sort()\n                        print(mids)\n                        if mids not in result :\n                            result.append(mids) \n                        break\n                elif j\/\/length==j\/length:\n                    j=int((j-length)\/length)\n                    mids.append(candidates&#91;-1])\n                else: \n                    mids.append(candidates&#91;j-(j\/\/length)*length-1])\n                    j=int(j\/\/length)\n              \n              mids=&#91;]\n        return result\n   # @lc code=end\n<\/code><\/pre>\n\n\n\n<p>\u663e\u793a\u8d85\u65f6\u4e86&#8230;..<\/p>\n\n\n\n<p>\u5176\u5b9e\u5927\u81f4\u53cd\u65b9\u5411\u5bf9\u7684\uff0c\u4f46\u662f\u6709\u4e9b\u5730\u65b9\u4e0d\u592a\u6b63\u786e<\/p>\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-239.png\" alt=\"\" class=\"wp-image-2144\" width=\"552\" height=\"315\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-239.png 978w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-239-300x172.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-239-768x440.png 768w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u5c1d\u8bd5\u7528\u56de\u6eaf\u7684\u65b9\u6cd5\uff08\u9012\u5f52\u8c03\u7528\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import List\r\n\r\n\r\nclass Solution:\r\n    def combinationSum(self, candidates: List&#91;int], target: int) -> List&#91;List&#91;int]]:\r\n\r\n        def dfs(candidates, begin, size, path, res, target):\r\n            if target &lt; 0:\r\n                return\r\n            if target == 0:\r\n                res.append(path)\r\n                return\r\n\r\n            for index in range(begin, size):\r\n                dfs(candidates, index, size, path + &#91;candidates&#91;index]], res, target - candidates&#91;index])\r\n\r\n        size = len(candidates)\r\n        if size == 0:\r\n            return &#91;]\r\n        path = &#91;]\r\n        res = &#91;]\r\n        dfs(candidates, 0, size, path, res, target)\r\n        return res<\/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-240.png\" alt=\"\" class=\"wp-image-2145\" width=\"527\" height=\"145\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-240.png 729w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-240-300x83.png 300w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/figure><\/div>\n\n\n\n<h3>\u526a\u679d\u63d0\u901f<\/h3>\n\n\n\n<ul><li>\u6839\u636e\u4e0a\u9762\u753b\u6811\u5f62\u56fe\u7684\u7ecf\u9a8c\uff0c\u5982\u679c&nbsp;<code>target<\/code>&nbsp;\u51cf\u53bb\u4e00\u4e2a\u6570\u5f97\u5230\u8d1f\u6570\uff0c\u90a3\u4e48\u51cf\u53bb\u4e00\u4e2a\u66f4\u5927\u7684\u6811\u4f9d\u7136\u662f\u8d1f\u6570\uff0c\u540c\u6837\u641c\u7d22\u4e0d\u5230\u7ed3\u679c\u3002\u57fa\u4e8e\u8fd9\u4e2a\u60f3\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8f93\u5165\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6dfb\u52a0\u76f8\u5173\u903b\u8f91\u8fbe\u5230\u8fdb\u4e00\u6b65\u526a\u679d\u7684\u76ee\u7684\uff1b<\/li><li>\u6392\u5e8f\u662f\u4e3a\u4e86\u63d0\u9ad8\u641c\u7d22\u901f\u5ea6\uff0c\u5bf9\u4e8e\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6765\u8bf4\u975e\u5fc5\u8981\u3002\u4f46\u662f\u641c\u7d22\u95ee\u9898\u4e00\u822c\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u80fd\u526a\u679d\u5c31\u5c3d\u91cf\u526a\u679d\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import List\r\n\r\n\r\nclass Solution:\r\n    def combinationSum(self, candidates: List&#91;int], target: int) -> List&#91;List&#91;int]]:\r\n\r\n        def dfs(candidates, begin, size, path, res, target):\r\n            if target == 0:\r\n                res.append(path)\r\n                return\r\n\r\n            for index in range(begin, size):\r\n                residue = target - candidates&#91;index]\n                #\r\u526a\u679d\u63d0\u901f\n                if residue &lt; 0:\r\n                    break\r\n\r\n                dfs(candidates, index, size, path + &#91;candidates&#91;index]], res, residue)\r\n\r\n        size = len(candidates)\r\n        if size == 0:\r\n            return &#91;]\r\n        candidates.sort()\r\n        path = &#91;]\r\n        res = &#91;]\r\n        dfs(candidates, 0, size, path, res, target)\r\n        return res\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;\u65e0\u91cd\u590d\u5143\u7d20&nbsp;\u7684\u6574\u6570\u6570\u7ec4&nbsp;candidates&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u6574\u6570 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/19\/leetcodeday39\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday39- \u6570\u7ec4\u603b\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\/2128"}],"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=2128"}],"version-history":[{"count":9,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2128\/revisions"}],"predecessor-version":[{"id":2147,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2128\/revisions\/2147"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2128"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}