16. 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://leetcode.com/problems/3sum-closest/
题解:
这道题跟3 sum很像,也是先对数组进行排序,然后通过3指针法对结果进行夹逼。
Time Complexity - O(n2),Space Complexity - O(1)。
public class Solution { public int threeSumClosest(int[] nums, int target) { if(nums == null || nums.length < 3) return 0; Arrays.sort(nums); int left, right, result = 0, diff = Integer.MAX_VALUE; for(int i = 0; i < nums.length - 2; i ++){ left = i + 1; right = nums.length - 1; while(left < right){ int sum = nums[i] + nums[left] + nums[right]; if(sum == target) return sum; else if(sum < target) left ++; else right --; if(Math.abs(sum - target) < diff){ diff = Math.abs(sum - target); result = sum; } } } return result; } }
二刷:
代码基本和3Sum一样,时间复杂度也一样。关键点还是去重的pruning,去重能节约不少时间。
Java:
Time Complexity - O(n2), Space Complexity - O(1)
public class Solution { public int threeSumClosest(int[] nums, int target) { if (nums == null || nums.length < 3) { return 0; } Arrays.sort(nums); int diff = Integer.MAX_VALUE, result = 0; for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } int lo = i + 1; int hi = nums.length - 1; while (lo < hi) { int sum = nums[i] + nums[lo] + nums[hi]; if (sum == target) { return sum; } else if (sum < target) { lo++; while (lo < hi && nums[lo] == nums[lo - 1]) { lo++; } } else { hi--; while (lo < hi && nums[hi] == nums[hi + 1]) { hi--; } } if (Math.abs(sum - target) < diff) { diff = Math.abs(sum - target); result = sum; } } } return result; } }
Python:
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ if not nums or len(nums) < 3: return 0 diff = 1 << 32 res = 0 nums.sort() for i in range(0, len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue lo = i + 1 hi = len(nums) - 1 while lo < hi: sum = nums[i] + nums[lo] + nums[hi] if sum == target: return sum elif sum < target: lo += 1 while lo < hi and nums[lo] == nums[lo - 1]: lo += 1 else: hi -= 1 while lo < hi and nums[hi] == nums[hi + 1]: hi -= 1 if abs(sum - target) < diff: diff = abs(sum - target) res = sum return res
三刷:
先对数组进行排序,初始化closest = 0,diff = Integer.MAX_VALUE, 然后遍历数组。注意要去重复,并且在计算完每个sum以后,要尝试更新closest。 最后返回结果。去重复包括遍历i的时候,以及lo++和hi--的时候。
Java:
Time Complexity - O(n2),Space Complexity - O(1)。
public class Solution { public int threeSumClosest(int[] nums, int target) { if (nums == null || nums.length < 3) return 0; Arrays.sort(nums); int closest = 0, diff = Integer.MAX_VALUE; for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int lo = i + 1, hi = nums.length - 1; int sum = 0; while (lo < hi) { sum = nums[i] + nums[lo] + nums[hi]; if (sum == target) { return sum; } else if (sum < target) { lo++; while (lo < hi && nums[lo] == nums[lo - 1]) lo++; } else { hi--; while (lo < hi && nums[hi] == nums[hi + 1]) hi--; } if (Math.abs(target - sum) < diff) { diff = Math.abs(target - sum); closest = sum; } } } return closest; } }
Reference: