【LeetCode】 16. 3Sum Closest 最接近的三数之和(Medium)(JAVA)
题目地址: https://leetcode.com/problems/3sum-closest/
题目描述:
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.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
题目大意
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
解题方法
1、对数组进行排序
2、固定一个元素,进行 for 循环
3、通过 while 循环,不断靠近 target,获得最新的元素
class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3) return 0;
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1;
int end = nums.length - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] == target) return target;
if (Math.abs(nums[i] + nums[start] + nums[end] - target) < Math.abs(res - target)) res = nums[i] + nums[start] + nums[end];
if (nums[start] + nums[end] > (target - nums[i])) {
end--;
} else {
start++;
}
}
}
return res;
}
}
执行用时 : 7 ms, 在所有 Java 提交中击败了 57.99% 的用户
内存消耗 : 38.7 MB, 在所有 Java 提交中击败了 5.04% 的用户
题目地址: https://leetcode.com/problems/3sum-closest/
题目描述:
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.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
题目大意
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
解题方法
1、对数组进行排序
2、固定一个元素,进行 for 循环
3、通过 while 循环,不断靠近 target,获得最新的元素
class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3) return 0;
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1;
int end = nums.length - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] == target) return target;
if (Math.abs(nums[i] + nums[start] + nums[end] - target) < Math.abs(res - target)) res = nums[i] + nums[start] + nums[end];
if (nums[start] + nums[end] > (target - nums[i])) {
end--;
} else {
start++;
}
}
}
return res;
}
}
执行用时 : 7 ms, 在所有 Java 提交中击败了 57.99% 的用户
内存消耗 : 38.7 MB, 在所有 Java 提交中击败了 5.04% 的用户
题目地址: https://leetcode.com/problems/3sum-closest/
题目描述:
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.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
题目大意
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
解题方法
1、对数组进行排序
2、固定一个元素,进行 for 循环
3、通过 while 循环,不断靠近 target,获得最新的元素
class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3) return 0;
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1;
int end = nums.length - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] == target) return target;
if (Math.abs(nums[i] + nums[start] + nums[end] - target) < Math.abs(res - target)) res = nums[i] + nums[start] + nums[end];
if (nums[start] + nums[end] > (target - nums[i])) {
end--;
} else {
start++;
}
}
}
return res;
}
}
执行用时 : 7 ms, 在所有 Java 提交中击败了 57.99% 的用户
内存消耗 : 38.7 MB, 在所有 Java 提交中击败了 5.04% 的用户