{"id":2881,"date":"2020-02-05T13:53:44","date_gmt":"2020-02-05T05:53:44","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2881"},"modified":"2020-02-05T13:53:44","modified_gmt":"2020-02-05T05:53:44","slug":"%e3%80%90leetcode%e3%80%910040-%e7%bb%84%e5%90%88%e6%80%bb%e6%95%b0ii","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2881\/","title":{"rendered":"\u3010LeetCode\u30110040 \u7ec4\u5408\u603b\u6570II"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/combination-sum-ii\/\">https:\/\/leetcode-cn.com\/problems\/combination-sum-ii\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def combinationSum(self, candidates, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> candidates: List[int]\n        <\/em><strong><em>:type<\/em><\/strong><em> target: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[List[int]]\n        \"\"\"\n        <\/em>candidates.sort()\n        n = len(candidates)\n        res = []\n        def backtrack(i, tmp_sum, tmp):\n            <em>\"\"\"\n            \u56de\u6eaf\u6cd5\n            <\/em><strong><em>:param<\/em><\/strong><em> i: \u5f53\u524d\u9009\u62e9\u7684\u7d22\u5f15\n            <\/em><strong><em>:param<\/em><\/strong><em> tmp_sum: \u5f53\u524d\u5df2\u9009\u62e9\u6240\u6709\u6570\u7684\u548c\n            <\/em><strong><em>:param<\/em><\/strong><em> tmp: \u5f53\u524d\u5df2\u9009\u62e9\u6570\u7684\u5217\u8868\n            <\/em><strong><em>:return<\/em><\/strong><em>:\n            \"\"\"\n            <\/em>if tmp_sum > target or i == n + 1:\n                return\n            if tmp_sum == target:\n                res.append(tmp)\n                return\n            for j in range(i, n):\n                if tmp_sum + candidates[j] > target:\n                    break\n                backtrack(j + 1, tmp_sum + candidates[j], tmp + [candidates[j]])\n        backtrack(0, 0, [])\n        res_final = []\n        # \u53bb\u91cd\n        for each in res:\n            if each not in res_final:\n                res_final.append(each)\n        return res_final\nif __name__ == '__main__':\n    # candidates = [10, 1, 2, 7, 6, 1, 5]\n    # target = 8\n    candidates = [2, 5, 2, 1, 2]\n    target = 5\n    print(Solution().combinationSum(candidates, target))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u548c\u4e0a\u4e00\u9053\u9898\u5341\u5206\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u5c31\u662f\u9700\u8981\u505a\u4e00\u4e9b\u5904\u7406\u3002<\/p>\n\n\n<p>\u6bd4\u5982\u4e0a\u4e00\u9053\u9898\u5141\u8bb8\u4e00\u4e2a\u6570\u5b57\u4f7f\u7528\u591a\u6b21\uff0c\u56e0\u6b64\u9012\u5f52\u8c03\u7528\u7684\u65f6\u5019\u4f9d\u7136\u4f20\u5165j\uff0c\u4f46\u662f\u8fd9\u9053\u9898\u4e0d\u5141\u8bb8\u91cd\u590d\u4f7f\u7528\uff0c\u56e0\u6b64\u4e0b\u4e00\u6b21\u67e5\u627e\u5e94\u8be5\u662fj+1\u6b21\u3002<\/p>\n\n\n<p>\u4e0a\u4e00\u9053\u9898\u9898\u4e2d\u5199\u5230\u6ca1\u6709\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u4e00\u9053\u9898\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u5728\u8fd4\u56de\u524d\u8981\u505a\u4e00\u6b21\u53bb\u91cd\u3002<\/p>\n\n\n<p>\u4e0a\u4e00\u9053\u9898\u8fb9\u754c\u6761\u4ef6\u662fi==n\uff0c\u7406\u7531\u548c\u7b2c\u4e00\u6761\u5dee\u4e0d\u591a\uff0c\u56e0\u4e3a\u662f\u4f20\u5165j\uff0c\u800c\u8fd9\u9053\u9898\u662fj+1\uff0c\u56e0\u6b64\u8fb9\u754c\u6761\u4ef6\u4e5f\u5e94\u8be5\u53d8\u6210n+1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/combination-sum-i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1584,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2881"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2881"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2881\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2881"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}