leet-code/src/main/java/leetcode/editor/cn/ThreeSumClosest.java

72 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。
//
// 返回这三个数的和。
//
// 假定每组输入只存在恰好一个解。
//
//
//
// 示例 1
//
//
//输入nums = [-1,2,1,-4], target = 1
//输出2
//解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
//
//
// 示例 2
//
//
//输入nums = [0,0,0], target = 1
//输出0
//
//
//
//
// 提示:
//
//
// 3 <= nums.length <= 1000
// -1000 <= nums[i] <= 1000
// -10⁴ <= target <= 10⁴
//
// Related Topics 数组 双指针 排序 👍 1086 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//16:最接近的三数之和
public class ThreeSumClosest {
public static void main(String[] args) {
Solution solution = new ThreeSumClosest().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left != right) {
int sum = nums[i] + nums[left] + nums[right];
if (Math.abs(sum - target) < Math.abs(res - target)) {
res = sum;
}
if (sum > target) {
right--;
} else {
left++;
}
}
}
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}