We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents d5af3d2 + 939bb18 commit 229181cCopy full SHA for 229181c
solution/016. 3Sum Closest/Solution.java
@@ -0,0 +1,17 @@
1
+class Solution {
2
+ public int threeSumClosest(int[] nums, int target) {
3
+ int result = nums[0]+nums[1]+nums[2];
4
+ Arrays.sort(nums);
5
+ for(int i = 0;i<nums.length-2;i++){
6
+ int start = i+1,end=nums.length-1;
7
+ while(start<end){
8
+ int cache = nums[i]+nums[start]+nums[end];
9
+ if(Math.abs(cache-target)<Math.abs(result-target)) result = cache;
10
+ if(cache < target ) start++;
11
+ else if(cache > target) end--;
12
+ else return result;
13
+ }
14
15
+ return result;
16
17
+}
0 commit comments