{"id":4048,"date":"2018-09-19T09:08:44","date_gmt":"2018-09-19T16:08:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4048"},"modified":"2018-09-19T09:18:58","modified_gmt":"2018-09-19T16:18:58","slug":"leetcode-16-3sum-closest","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-16-3sum-closest\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 16. 3Sum Closest"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>nums<\/code>\u00a0of\u00a0<em>n<\/em>\u00a0integers and an integer\u00a0<code>target<\/code>, find three integers in\u00a0<code>nums<\/code>\u00a0such that the sum is closest to\u00a0<code>target<\/code>. Return the sum of the three integers. You may assume that each input would have exactly one solution.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\">Given array nums = [-1, 2, 1, -4], and target = 1.\r\n\r\nThe sum that is closest to the target is 2. (-1 + 2 + 1 = 2).\r\n<\/pre>\n<h1><strong>Solution: Sorting + Two Pointers<\/strong><\/h1>\n<p>Similar to\u00a0<a href=\"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-15-3sum\/\">\u82b1\u82b1\u9171 LeetCode 15. 3Sum<\/a><\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 8 ms\r\nclass Solution {\r\npublic:\r\n  int threeSumClosest(vector&lt;int&gt; &amp;num, int target) {\r\n    int n = num.size();\r\n    int d = INT_MAX;\r\n    int ans = target;\r\n    sort(num.begin(), num.end());\r\n      \r\n    for (int i = 0; i &lt; n - 2; i++) {\r\n        int s = i + 1, t = n - 1;\r\n        while (s &lt; t) {\r\n          int sum = num[i] + num[s] + num[t];\r\n          if (sum == target)\r\n              return target;            \r\n\r\n          int diff = abs(sum - target);\r\n          if (diff &lt; d) {\r\n            d = diff;\r\n            ans = sum;\r\n          }\r\n\r\n          if (sum &gt; target) --t;            \r\n          else ++s;\r\n        }\r\n      }\r\n\r\n      return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\n  public int threeSumClosest(int[] nums, int target) {\r\n    Arrays.sort(nums);\r\n    int n = nums.length;\r\n    int ans = 0;\r\n    int d = Integer.MAX_VALUE;\r\n    for (int i = 0; i &lt; n - 2; ++i) {\r\n      int s = i + 1;\r\n      int t = n - 1;\r\n      while (s &lt; t) {\r\n        int sum = nums[i] + nums[s] + nums[t];\r\n        if (sum == target) return target;\r\n        int diff = Math.abs(sum - target);\r\n        if (diff &lt; d) {\r\n          d = diff;\r\n          ans = sum;\r\n        }\r\n        if (sum &gt; target) --t;\r\n        else ++s;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true  \"># Author: Huahua\r\nclass Solution:\r\n  def threeSumClosest(self, nums, target):\r\n    nums.sort()\r\n    n = len(nums)\r\n    d = 2**32\r\n    ans = 0\r\n    for i in range(n - 2):\r\n      s, t = i + 1, n - 1\r\n      while s &lt; t:\r\n        sum3 = nums[i] + nums[s] + nums[t]\r\n        if sum3 == target: return target\r\n        diff = abs(sum3 - target)\r\n        if diff &lt; d: ans, d = sum3, diff        \r\n        if sum3 &gt; target: \r\n          t -= 1 \r\n        else: \r\n          s += 1\r\n    return ans<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0nums\u00a0of\u00a0n\u00a0integers and an integer\u00a0target, find three integers in\u00a0nums\u00a0such that the sum is closest to\u00a0target. Return the sum of the three integers. You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[237,175],"class_list":["post-4048","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-3sum","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4048"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4048\/revisions"}],"predecessor-version":[{"id":4051,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4048\/revisions\/4051"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}