Find three integers in nums such that the sum is closest to target

Problem description

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.

Examples

1
2
3
Example 1:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Solution

  知识迁移能力,一开始仅是在上道题The sum of three elements is zero加了个判断,找到最小偏差。但是这样直接用是不对的.因为找到最小偏差时,不能同时移动j,k要判断才能移动索引。若sum > target,k–;sum < target,j++;sum == target ,return target;矛盾的特殊性啊~~~ 具体情况具体分析。不要死板。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int j, k;
int closetSum = 0;
int minOffset = Integer.MAX_VALUE;
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
j = i + 1;
k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
int offset = Math.abs(sum - target);
if (sum > target) {
if (offset < minOffset) {
minOffset = offset;
closetSum = sum;
while (j < k && nums[k - 1] == nums[k]) {
k--;
}
}
k--;
} else if (sum < target) {
if (offset < minOffset) {
minOffset = offset;
closetSum = sum;
while (j < k && nums[j + 1] == nums[j]) {
j++;
}
}
j++;
} else {
return target;
}
}
}
return closetSum;
}
文章目录
  1. 1. Problem description
    1. 1.1. 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.
  2. 2. Examples
  3. 3. Solution
  4. 4. Code
|