{"id":2794,"date":"2020-01-19T12:58:02","date_gmt":"2020-01-19T04:58:02","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2794"},"modified":"2020-01-19T12:58:02","modified_gmt":"2020-01-19T04:58:02","slug":"%e3%80%90leetcode%e3%80%910016-%e6%9c%80%e6%8e%a5%e8%bf%91%e7%9a%84%e4%b8%89%e6%95%b0%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2794\/","title":{"rendered":"\u3010LeetCode\u30110016 \u6700\u63a5\u8fd1\u7684\u4e09\u6570\u4e4b\u548c"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/3sum-closest\/\">https:\/\/leetcode-cn.com\/problems\/3sum-closest\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def threeSumClosest(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>: int\n        \"\"\"\n        <\/em>dis = float('inf')\n        res = []\n        nums.sort()\n        for i in range(len(nums) - 2):\n            left = i + 1\n            right = len(nums) - 1\n            while left &lt; right:\n                if abs(nums[i] + nums[left] + nums[right] - target) &lt; dis:\n                    dis = abs(nums[i] + nums[left] + nums[right] - target)\n                    res = [nums[i], nums[left], nums[right]]\n                    # \u5224\u65ad\u6307\u9488\u79fb\u52a8\u65b9\u5411\n                if nums[i] + nums[left] + nums[right] - target &lt; 0:\n                    left += 1\n                elif nums[i] + nums[left] + nums[right] - target > 0:\n                    right -= 1\n                else:\n                    return target\n        return sum(res)\nif __name__ == '__main__':\n    nums = [-1, 2, 1, -4]\n    nums = [1, 2, 4, 8, 16, 32, 64, 128]\n    target = 82\n    print(Solution().threeSumClosest(nums, target))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u548c\u4e0a\u9762\u7684\u90a3\u4e2a\u4e09\u6570\u4e4b\u548c\u5341\u5206\u76f8\u4f3c\uff0c\u4e00\u6837\u662f\u4f7f\u7528\u6307\u9488\u641c\u7d22\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u518d\u4ee5\u548c\u4e3a0\u4f5c\u4e3a\u5224\u65ad\uff0c\u5bf9\u5e94\u7684\u5f15\u5165\u4e00\u4e2a\u53d8\u91cfdis\u7528\u6765\u8868\u793a\u5f53\u524d\u96c6\u5408\u548ctarget\u7684\u8ddd\u79bb\uff0c\u540c\u65f6\u6839\u636e\u5f53\u524d\u96c6\u5408\u7684\u548c \u548c target\u7684\u5927\u5c0f\u5173\u7cfb\u51b3\u5b9a\u6307\u9488\u7684\u79fb\u52a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/3sum-closest\/ # &#8211; [&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":1366,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2794"}],"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=2794"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2794\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2794"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}