-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (33 loc) · 1.02 KB
/
Copy pathsolution.java
File metadata and controls
34 lines (33 loc) · 1.02 KB
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
// 16. 3Sum Closest
// https://leetcode.com/problems/3sum-closest/
// Medium | Java | Accepted 2026-02-08
// Runtime 17 ms | Memory 45.8 MB
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int currBest = 10000000;
int num = 0;
for(int i = 0; i<nums.length; i++)
{
int point1 = i+1;
int point2 = nums.length-1;
while(point1<point2)
{
if(Math.abs(nums[point1]+nums[point2]+nums[i]-target)<currBest)
{
currBest = Math.abs(nums[point1]+nums[point2]+nums[i]-target);
num = nums[point1]+nums[point2]+nums[i];
}
if(nums[point1]+nums[point2]+nums[i]>target)
{
point2--;
}
else
{
point1++;
}
}
}
return num;
}
}