difficulty: Medium
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
题意:
给一个数列,和一个整数target,在数列中求出三个数之和sum,并使得sum与target之差最小。数列保证只有一个最优解。
分析:
本题与上一题相似度非常高,可以使用相同的解题思路:
- 输入数列为乱序,首先进行排序。
- 一次循环遍历每个数,计算当前数和当前数的下一个及最后一个的和,判断当前和是否为最小,如果是则替换记录最小值。
- 按照上述所求的的和判断 后续搜索位置,另外还有一个特例 当最小值为0时,可以终止循环,因为题目保证仅有唯一最优解。
- 过滤掉相邻且相等的元素,可以避免不必要的计算。
- 实现如下。
Code & C++
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int slen = nums.size();
if(slen == 3)return nums[0]+nums[1]+nums[2];
int smin=1<<20,ans=0;
for(int i=0;i<slen;++i){
int first = nums[i], front=i+1, back=slen-1;
int tmp = target-first;
while( front< back){
int sum3 = nums[front] + nums[back];
if(sum3<tmp){
++front;
}else if(sum3>tmp){
--back;
}else{
return target;
}
if(smin>abs(tmp-sum3)){
ans = sum3 + nums[i];
smin = abs(tmp-sum3);
}
}
while(i<slen-1 && nums[i]==nums[i+1])++i;
}
return ans;
}
};
结果:
Runtime: 8 ms, faster than 100.00% of C++ online submissions for 3Sum Closest.
Memory Usage: 9.4 MB, less than 73.26% of C++ online submissions for 3Sum Closest.
Code & Python
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
slen = len(nums)
if slen==0:return
nums.sort()
i, smin, ans=0, 1<<30, 0
while i<slen:
first ,front ,back = nums[i], i+1, slen-1
while front<back:
sum = first + nums[front] + nums[back]
if smin > abs(target-sum):
smin = abs(target-sum)
ans = sum
if smin == 0:break
if sum < target:
if front<back and nums[front]==nums[front+1]:
while front<back and nums[front]==nums[front+1]:front+=1
else:
front+=1
else:
if back>front and nums[back]==nums[back-1]:
while front<back and nums[back]==nums[back-1]: back-=1
else:
back-=1
if smin==0:
break
i+=1
while i<slen and nums[i]==nums[i-1]:i+=1
return ans
结果:
Runtime: 100 ms, faster than 84.33% of Python3 online submissions for 3Sum Closest.
Memory Usage: 13.1 MB, less than 5.29% of Python3 online submissions for 3Sum Closest.
Status:Accepted