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.
1 2
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).
分析
其实思路和这道题是一样的,首先排序,从左到右由小到大的顺序,通过第一层循环确定第一个数,其他两个在第一个数的右边(都比第一个数大)取,两个数从两侧向中间靠拢,如果三数相加与target的差的绝对值相差比 result 更小,就更新,三数相加大于 target, 第三个数向左移动(减小), 否则,第二个数向右移动。时间复杂度$O(n^2)$,空间复杂度是$O(1)$,因为排序是$nlogn$,查找是$O(n^2)$。
impl Solution { pubfnthree_sum_closest(nums: Vec<i32>, target: i32) -> i32 { letmut nums = nums; letmut result: i32 = i32::max_value(); nums.sort(); letmut i = 0; while i < nums.len() - 2 { let now_min = Solution::two_sum_closest(&nums[(i+1)..nums.len()], target - nums[i]); if now_min.abs() < result.abs() { result = now_min; if result == 0 { break; } } i += 1 } target + result }
fntwo_sum_closest(nums: &[i32], target: i32) -> i32 { let (mut i, mut j) = (0_usize, nums.len() - 1); letmut result = i32::max_value(); while i < j { let sum = nums[i] + nums[j]; if sum > target { j -= 1; } elseif sum < target { i += 1; } else { return0; } if (sum - target).abs() < result.abs() { result = sum - target } } result } }