Skip to content

Commit c8715ff

Browse files
author
zhangbo54
committed
16. 3Sum Closest
1 parent 2dcf926 commit c8715ff

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/leetcode/_16_/Main.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package leetcode._16_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
/**
13+
* Created by zhangbo54 on 2019-03-04.
14+
*/
15+
public class Main {
16+
public static void main(String[] args) {
17+
Solution solution = new Solution();
18+
System.out.println(solution.threeSumClosest(new int[]{4,0,5,-5,3,3,0,-4,-5}, -2));
19+
20+
// Integer.parseInt("1" + Integer.MAX_VALUE);
21+
// -4 -1, -1, 0, 1, 2,
22+
}
23+
}
24+
25+
class Solution {
26+
public int threeSumClosest(int[] nums, int target) {
27+
int sum = 0;
28+
if (nums.length < 3) {
29+
return sum;
30+
}
31+
Arrays.sort(nums);
32+
int minGap = nums[0] + nums[1] + nums[2] - target;
33+
for (int i = 0; i < nums.length - 1; i++) {
34+
if (nums[i] > 0 && nums[i] > target) {
35+
continue;
36+
}
37+
if (i > 1 && nums[i] == nums[i - 1]) {
38+
continue;
39+
}
40+
int numSum = target - nums[i];
41+
int x = i + 1;
42+
int y = nums.length - 1;
43+
while (x < y ) {
44+
int tempSum = nums[x] + nums[y];
45+
if (tempSum == numSum) {
46+
return target;
47+
} else {
48+
if (Math.abs(minGap) > Math.abs(tempSum - numSum)) {
49+
minGap = tempSum - numSum;
50+
}
51+
if (tempSum > numSum) {
52+
y--;
53+
} else {
54+
x++;
55+
}
56+
}
57+
}
58+
}
59+
return minGap + target;
60+
}
61+
}

src/leetcode/_16_/solution.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
### [16\. 3Sum Closest](https://leetcode.com/problems/3sum-closest/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
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.
7+
8+
**Example:**
9+
10+
```
11+
Given array nums = [-1, 2, 1, -4], and target = 1.
12+
13+
The sum that is closest to the target is 2\. (-1 + 2 + 1 = 2).
14+
```
15+
16+
17+
#### Solution
18+
19+
Language: **Java**
20+
21+
```java
22+
class Solution {
23+
   public int threeSumClosest(int[] nums, int target) {
24+
       int sum = 0;
25+
       if (nums.length < 3) {
26+
           return sum;
27+
      }
28+
       Arrays.sort(nums);
29+
       int minGap = nums[0] + nums[1] + nums[2] - target;
30+
       for (int i = 0; i < nums.length - 1; i++) {
31+
           if (nums[i] > 0 && nums[i] > target) {
32+
               continue;
33+
          }
34+
           if (i > 1 && nums[i] == nums[i - 1]) {
35+
               continue;
36+
          }
37+
           int numSum = target - nums[i];
38+
           int x = i + 1;
39+
           int y = nums.length - 1;
40+
           while (x < y ) {
41+
               int tempSum = nums[x] + nums[y];
42+
               if (tempSum == numSum) {
43+
                   return target;
44+
              } else {
45+
                   if (Math.abs(minGap) > Math.abs(tempSum - numSum)) {
46+
                       minGap = tempSum - numSum;
47+
                  }
48+
                   if (tempSum > numSum) {
49+
                       y--;
50+
                  } else {
51+
                       x++;
52+
                  }
53+
              }
54+
          }
55+
      }
56+
       return minGap + target;
57+
  }
58+
}
59+
                   if (Math.abs(gap) > Math.abs(tempSum - numSum)) {
60+
```

0 commit comments

Comments
 (0)