给你一个长度为 n
的整数数组 nums
和 一个目标值 target
。请你从 nums
中选出三个整数,使它们的和与 target
最接近。
返回这三个数的和。
假定每组输入只存在恰好一个解。
输入:
nums = [-1,2,1,-4], target = 1
输出:
2
解释:
与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
输入:
nums = [0,0,0], target = 1
输出:
0
https://leetcode.cn/problems/3sum-closest/
impl Solution {
pub fn three_sum_closest(mut nums: Vec<i32>, target: i32) -> i32 {
let n = nums.len();
nums.sort();
let mut ans = 23001;
for f in 0..n {
if f == 0 || nums[f] != nums[f - 1] {
let mut s = f + 1;
let mut t = n - 1;
while s < t {
let sum = nums[f] + nums[s] + nums[t];
if sum == target {
return target;
}
if (sum - target).abs() < (ans - target).abs() {
ans = sum;
}
if sum > target {
let mut t0 = t - 1;
while s < t0 && nums[t0] == nums[t] {
t0 -= 1;
}
t = t0;
} else {
let mut s0 = s + 1;
while s0 < t && nums[s0] == nums[s] {
s0 += 1;
}
s = s0;
}
}
}
}
return ans;
}
}
func threeSumClosest(nums []int, target int) int {
n := len(nums)
sort.Ints(nums)
ans := 23001
abs := func(num int) int {
if num < 0 {
return -num
}
return num
}
for f := 0; f < n; f++ {
if f > 0 && nums[f] == nums[f-1] {
continue
}
s, t := f+1, n-1
for s < t {
sum := nums[f] + nums[s] + nums[t]
if sum == target {
return target
}
if abs(sum-target) < abs(ans-target) {
ans = sum
}
if sum > target {
t0 := t - 1
for s < t0 && nums[t0] == nums[t] {
t0 -= 1
}
t = t0
} else {
s0 := s + 1
for s0 < t && nums[s0] == nums[s] {
s0 += 1
}
s = s0
}
}
}
return ans
}
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
sort(nums.begin(), nums.end());
int ans = 23001;
for (int f = 0; f < n; ++f) {
if (f > 0 && nums[f] == nums[f - 1]) {
continue;
}
int s = f + 1;
int t = n - 1;
while (s < t) {
int sum = nums[f] + nums[s] + nums[t];
if (sum == target) {
return target;
}
if (abs(sum - target) < abs(ans - target)) {
ans = sum;
}
if (sum > target) {
int t0 = t - 1;
while (s < t0 && nums[t0] == nums[t]) {
t0 -= 1;
}
t = t0;
} else {
int s0 = s + 1;
while (s0 < t && nums[s0] == nums[s]) {
s0 += 1;
}
s = s0;
}
}
}
return ans;
}
};
class Solution {
public int threeSumClosest(int[] nums, int target) {
int n = nums.length;
Arrays.sort(nums);
int ans = 23001;
for (int f = 0; f < n; ++f) {
if (f > 0 && nums[f] == nums[f - 1]) {
continue;
}
int s = f + 1;
int t = n - 1;
while (s < t) {
int sum = nums[f] + nums[s] + nums[t];
if (sum == target) {
return target;
}
if (Math.abs(sum - target) < Math.abs(ans - target)) {
ans = sum;
}
if (sum > target) {
int t0 = t - 1;
while (s < t0 && nums[t0] == nums[t]) {
t0 -= 1;
}
t = t0;
} else {
int s0 = s + 1;
while (s0 < t && nums[s0] == nums[s]) {
s0 += 1;
}
s = s0;
}
}
}
return ans;
}
}
function threeSumClosest(nums: number[], target: number): number {
const n = nums.length;
nums.sort((a, b) => a - b);
let ans = 23001;
for (let f = 0; f < nums.length; ++f) {
if (f > 0 && nums[f] === nums[f - 1]) {
continue;
}
let s = f + 1;
let t = n - 1;
while (s < t) {
const sum = nums[f] + nums[s] + nums[t];
if (sum === target) {
return target;
}
if (Math.abs(sum - target) < Math.abs(ans - target)) {
ans = sum;
}
if (sum > target) {
let t0 = t - 1;
while (s < t0 && nums[t0] === nums[t]) {
t0 -= 1;
}
t = t0;
} else {
let s0 = s + 1;
while (s0 < t && nums[s0] === nums[s]) {
s0 += 1;
}
s = s0;
}
}
}
return ans;
};
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
n = len(nums)
nums.sort()
ans = 23001
for f in range(n):
if f > 0 and nums[f] == nums[f - 1]:
continue
s = f + 1
t = n - 1
while s < t:
sum = nums[f] + nums[s] + nums[t]
if sum == target:
return target
if abs(sum - target) < abs(ans - target):
ans = sum
if sum > target:
t0 = t - 1
while s < t0 and nums[t0] == nums[t]:
t0 -= 1
t = t0
else:
s0 = s + 1
while s0 < t and nums[s0] == nums[s]:
s0 += 1
s = s0
return ans
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~