【Leetcode】3Sum Closest

题目链接:https://leetcode.com/problems/3sum-closest/

题目:

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思路:

参考http://blog.csdn.net/yeqiuzs/article/details/50272757 解题框架,本题要求跟target距离最近的3元素之和,所以要保存最小距离、最小距离时候的3元素的和。
   这里要搞清楚一点,在判断一个已排序数组中是否存在两元素之和 target的时候,要注意可以从两边向中间逼近target,时空复杂度为O(N),而不是两层for循环两个指针指向两个元素判断是否为target,时空复杂度为O(N^2)。如果是无序数组快排后再向中间逼近是O(N logN),也比O(N^2)好。  本题解法的时间复杂度为O(N^2)。

算法:

[java] view
plain
 copy

  1. public int threeSumClosest(int[] nums, int target) {
  2. Arrays.sort(nums);
  3. int min = Integer.MAX_VALUE,x = target; //min为target和3个元素和的最小距离,x是3元素和
  4. for (int i1 = 0; i1 < nums.length; i1++) {
  5. int i2 = i1 + 1;
  6. int i3 = nums.length - 1;
  7. while (i2 < i3) {
  8. if (Math.abs(nums[i2] + nums[i3] + nums[i1] - target) < min) {
  9. min = Math.abs(nums[i2] + nums[i3] + nums[i1] - target);
  10. x = nums[i2] + nums[i3] + nums[i1];//保存距离最近的和
  11. }
  12. if (nums[i2] + nums[i3] + nums[i1] - target > 0) {
  13. i3--;
  14. } else if (nums[i2] + nums[i3] + nums[i1] - target <= 0) {
  15. i2++;
  16. }
  17. }
  18. }
  19. return x;
  20. }

时间: 2024-10-13 02:33:01

【Leetcode】3Sum Closest的相关文章

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【leetcode】3Sum Closest(middle)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【Leetcode】3Sum

题目链接:https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in no

【leetcode】3Sum Closet

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【leetcode】3Sum (medium)

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted

LeetCode 016 3Sum Closest

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {