16:最接近的三数之和
This commit is contained in:
parent
8a2a98e5bd
commit
29ec6d9681
72
src/main/java/leetcode/editor/cn/ThreeSumClosest.java
Normal file
72
src/main/java/leetcode/editor/cn/ThreeSumClosest.java
Normal file
@ -0,0 +1,72 @@
|
||||
//给你一个长度为 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)
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code><em> </em>和 一个目标值 <code>target</code>。请你从 <code>nums</code><em> </em>中选出三个整数,使它们的和与 <code>target</code> 最接近。</p>
|
||||
|
||||
<p>返回这三个数的和。</p>
|
||||
|
||||
<p>假定每组输入只存在恰好一个解。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,2,1,-4], target = 1
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,0,0], target = 1
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 1000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>排序</li></div></div><br><div><li>👍 1086</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user