标签:链接 href solution ble mat tar ret ref 3sum
class Solution {
public int threeSumClosest(int[] nums, int target) {
int ans = nums[0] + nums[1] + nums[2];
Arrays.sort(nums);
int n = nums.length;
for (int i = 0; i < n; ++i) {
int l = i + 1, r = n - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (Math.abs(sum - target) < Math.abs(ans - target)) {
ans = sum;
}
if (sum == target) {
return target;
} else if (sum < target) l++;
else r--;
}
}
return ans;
}
}
标签:链接 href solution ble mat tar ret ref 3sum
原文地址:https://www.cnblogs.com/clown9804/p/13117934.html