{"id":2798,"date":"2020-01-20T15:48:10","date_gmt":"2020-01-20T07:48:10","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2798"},"modified":"2020-01-20T15:48:10","modified_gmt":"2020-01-20T07:48:10","slug":"%e3%80%90leetcode%e3%80%910018-%e5%9b%9b%e6%95%b0%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2798\/","title":{"rendered":"\u3010LeetCode\u30110018 \u56db\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/4sum\/\">https:\/\/leetcode-cn.com\/problems\/4sum\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def fourSum(self, nums, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> nums: 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>nums.sort()\n        res = []\n        # \u9009\u5b9aa\n        for i in range(len(nums)):\n            # \u9009\u5b9ab\n            if i > 0 and nums[i] == nums[i- 1]:\n                continue\n            new_target = target - nums[i]\n            new_nums = nums[i + 1:]\n            for j in range(len(new_nums) - 2):\n                left = j + 1\n                right = len(new_nums) - 1\n                if j > 0 and new_nums[j] == new_nums[j - 1]:\n                    continue\n                while left &lt; right:\n                    temp = new_nums[j] + new_nums[left] + new_nums[right]\n                    if temp == new_target:\n                        res_list = [nums[i], new_nums[j], new_nums[left], new_nums[right]]\n                        res_list.sort()\n                        res.append(res_list)\n                        left += 1\n                        right -= 1\n                        # \u521d\u6b65\u53bb\u91cd\uff08\u76f8\u5f53\u4e8e\u526a\u679d\uff1f\uff09\n                        while left &lt; right and new_nums[left] == new_nums[left - 1]:\n                            left += 1\n                        while left &lt; right and new_nums[right] == new_nums[right + 1]:\n                            right -= 1\n                    if temp > new_target:\n                        right -= 1\n                    if temp &lt; new_target:\n                        left += 1\n        return res\nif __name__ == '__main__':\n    nums = [-3, -2, -1, 0, 0, 1, 2, 3]\n    target = 0\n    print(Solution().fourSum(nums, target))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u548c\u4e4b\u524d\u4e09\u6570\u4e4b\u548c\u5927\u76f8\u5f84\u5ead\uff0c\u53ea\u4e0d\u8fc7\u53ef\u4ee5\u628aa+b+c+d=target\u53d8\u6210b+c+d=target-a\u5373\u53ef\u3002\u540c\u65f6\u7531\u4e8e\u9700\u8981\u5faa\u73afa\u548cb\u7684\u503c\uff0c\u6240\u4ee5\u5728\u6bcf\u6b21\u5faa\u73af\u4e4b\u524d\u90fd\u8981\u5224\u65ad\u5f53\u524d\u7d22\u5f15\u7684ab\u503c\u548c\u4e0a\u4e00\u6b21\u7684\u4e00\u4e0d\u4e00\u6837\uff08if i > 0 and nums[i] == nums[i- 1]\uff09\u3002<\/p>\n\n\n<p><strong>\u601d\u8003<\/strong>\uff1a\u540c\u65f6\u5728\u8bc4\u8bba\u4e2d\u6ce8\u610f\u5230\u4e86\u4e00\u4e2a\u975e\u5e38\u6709\u610f\u601d\u7684\u526a\u679d\u64cd\u4f5c\uff0c\u5c31\u662f\u7528\u6700\u5927\u548c\u6700\u5c0f\u53ef\u80fd\u51fa\u73b0\u7684\u503c\u4f5c\u4e3a\u526a\u679d\uff0c\u5982\u5f53\u9009\u5b9a\u7d22\u5f150\u4e3aa\u65f6\uff0c\u8ba1\u7b970\u30011\u30012\u30013\u8fd9\u56db\u4e2a\u6570\u7684\u548c\u2014\u2014\u4e5f\u5c31\u662fabcd\u548c\u7684\u7406\u8bba\u6700\u5c0f\u503c\uff0c\u5982\u679c\u4e0d\u5728\u7ed3\u679c\u8303\u56f4\u5219\u76f4\u63a5\u8df3\u8fc7\u3001\u540c\u7406\u5904\u7406\u6700\u5927\u503c\u3002\u53ef\u4ee5\u8282\u7ea6\u975e\u5e38\u591a\u7684\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/4sum\/ # -*- codin [&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":1305,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2798"}],"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=2798"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2798\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2798"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}