题目描述
给定一个包括 n 个整数的数组nums
和 一个目标值target
。找出 nums
中的三个整数,使得它们的和与 target
最接近。返回这三个数的和。假定每组输入只存在唯一答案。
示例:
输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
C实现1–(冒泡排序+双指针)
/**
@algorithm description:
LeetCode16: 最接近的三数之和
step1: 对输入的数组进行从小到大的冒泡排序
step2: 利用for循环遍历第一个数的所有可能取值,利用双指针遍历所有不重复的三数组合,找出满足条件的三数组合
@Time complexity of the algorithm: O(N^2)
@Spatial complexity of the algorithm: O(N) 不允许改变输入的数组的情况下
**/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/**
@function: Bubble sort the input array
@param[in]: nums: The address of the array
@param[in]: numsSize: The size of the array
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
**/
void BubbleSort(int* nums, int numsSize)
{
int temp = 0;
for (int i = 0; i < numsSize; i++)
{
for (int j = 0; j < numsSize - i - 1; j++)
{
if (nums[j + 1] < nums[j])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
/**
@function: Find the case where the sum of three elements in an array is closest to target
@param[in]: nums: The address of the array
@param[in]: numsSize: The size of the array
@param[in]: target: The target value to be close to
@param[out]: NA
@return: ret: The value closest to target
@author: Cohen
@data: 2021/09/14
**/
int threeSumCloset(int* nums, int numsSize, int target)
{
int ret = 0;
int signalret = 0;
BubbleSort(nums, numsSize);
for (int i = 0; i < numsSize; i++)
{
int pre = i + 1;
int last = numsSize - 1;
int temp = 0;
while (pre < last)
{
temp = nums[i] + nums[pre] + nums[last];
if (temp == target)
{
return temp;
}
if (i == 0 && pre == 1 && last == (numsSize - 1))
{
signalret = temp;
}
else if (abs(temp - target) < abs(signalret - target))
{
signalret = temp;
}
if (temp > target)
{
last--;
}
else
{
pre++;
}
}
if (i == 0)
{
ret = signalret;
}
else if (i > 0 && abs(signalret - target) < abs(ret - target))
{
ret = signalret;
}
}
return ret;
}
/**
@function: A test case
@param[in]: NA
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
**/
void test16()
{
int a[5] = { 1,2,5,10,11 };
int ret = threeSumCloset(a, 5, 12);
printf("ret = %d\n", ret);
}
int main()
{
test16();
system("pause");
return 0;
}
C实现2–qsort()
排序+双指针
/**
@algorithm description:
LeetCode16: 最接近的三数之和
step1: 对输入的数组进行从小到大的qsort排序
step2: 利用for循环遍历第一个数的所有可能取值,利用双指针遍历所有不重复的三数组合,找出满足条件的三数组合
@Time complexity of the algorithm: O(N^2)
@Spatial complexity of the algorithm: O(N) 不允许改变输入的数组的情况下
**/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/**
@function: The 'compare' argument of qsort function
@return: #1.升序(从小到大排序): return (*(int*)a - *(int*)b)
#2.降序(从大到小排序): return (*(int*)b - *(int*)a)
@author: Cohen
@data: 2021/09/14
**/
int compare(const void* a, const void* b)
{
return (*(int*)a - *(int*)b);
}
/**
@function: Find the case where the sum of three elements in an array is closest to target
@param[in]: nums: The address of the array
@param[in]: numsSize: The size of the array
@param[in]: target: The target value to be close to
@param[out]: NA
@return: ret: The value closest to target
@author: Cohen
@data: 2021/09/14
**/
int threeSumCloset(int* nums, int numsSize, int target)
{
int ret = 0;
int signalret = 0;
qsort(nums, numsSize, sizeof(int), compare);
for (int i = 0; i < numsSize; i++)
{
int pre = i + 1;
int last = numsSize - 1;
int temp = 0;
while (pre < last)
{
temp = nums[i] + nums[pre] + nums[last];
if (temp == target)
{
return temp;
}
if (i == 0 && pre == 1 && last == (numsSize - 1))
{
signalret = temp;
}
else if (abs(temp - target) < abs(signalret - target))
{
signalret = temp;
}
if (temp > target)
{
last--;
}
else
{
pre++;
}
}
if (i == 0)
{
ret = signalret;
}
else if (i > 0 && abs(signalret - target) < abs(ret - target))
{
ret = signalret;
}
}
return ret;
}
/**
@function: A test case
@param[in]: NA
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
**/
void test16()
{
int a[5] = { 1,2,5,10,11 };
int ret = threeSumCloset(a, 5, 12);
printf("ret = %d\n", ret);
}
int main()
{
test16();
system("pause");
return 0;
}
C++实现–排序+双指针
/**
@algorithm description:
LeetCode16: 最接近的三数之和
step1: 对输入的数组进行从小到大的qsort排序
step2: 利用for循环遍历第一个数的所有可能取值,利用双指针遍历所有不重复的三数组合,找出满足条件的三数组合
@Time complexity of the algorithm: O(N^2)
@Spatial complexity of the algorithm: O(N) 不允许改变输入的数组的情况下
**/
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
/**
@function: The 'compare' argument of sort function in CPP
@return: #1.升序(从小到大排序): 默认
#2.降序(从大到小排序): return a > b
@author: Cohen
@warning: Need include header file <algorithm>
@data: 2021/09/14
**/
int compare(int a, int b)
{
return a > b;
}
/**
@function: Bubble sort the input array
@param[in]: nums: The address of the array
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
**/
void BubbleSort(vector<int>& nums)
{
int temp = 0;
int len = nums.size();
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (nums[j + 1] < nums[j])
{
temp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = temp;
}
}
}
}
/**
@function: Find the case where the sum of three elements in an array is closest to target
@param[in]: nums: The address of the array
@param[in]: target: The target value to be close to
@param[out]: NA
@return: ret: The value closest to target
@author: Cohen
@data: 2021/09/14
**/
int threeSumClosest(vector<int>& nums, int target)
{
int ret = 0;
int temp = 0;
int singleret = 0;
int len = nums.size();
int pre = 0;
int last = 0;
//sort(nums.begin(), nums.end());
BubbleSort(nums);
for (int i = 0; i < len; i++)
{
pre = i + 1;
last = len - 1;
while (pre < last)
{
temp = nums[i] + nums[pre] + nums[last];
if (temp == target)
{
return temp;
}
if (i == 0 && pre == 1 && last == (len - 1))
{
singleret = temp;
}
else if(abs(temp - target) < abs(singleret - target))
{
singleret = temp;
}
if (temp > target)
{
last--;
}
else
{
pre++;
}
if (i == 0)
{
ret = singleret;
}
else if (i > 0 && abs(singleret - target) < abs(ret - target))
{
ret = singleret;
}
}
}
return ret;
}
/**
@function: A test case
@param[in]: NA
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
**/
void test16()
{
vector<int> a;
a.push_back(-1);
a.push_back(2);
a.push_back(1);
a.push_back(-4);
int ret = threeSumClosest(a,1);
cout << "ret = " << ret << endl;
}
int main()
{
test16();
system("pause");
return 0;
}
Python实现–sort() + 双指针
#!/user/bin/env python
# -*- coding:utf-8 -*-
#@Time : 2021/9/14 22:24
#@Author : Cohen
#@File : leetcode16.py
"""
@function deccription:
LeetCode16: 最接近的三数之和
step1: 对输入的数组进行从小到大的qsort排序
step2: 利用for循环遍历第一个数的所有可能取值,利用双指针遍历所有不重复的三数组合,找出满足条件的三数组合
@Time complexity of the algorithm: O(N^2)
@Spatial complexity of the algorithm: O(N) 不允许改变输入的数组的情况下
"""
"""
@function: Find the case where the sum of three elements in an array is closest to target
@param[in]: nums: The address of the array
@param[in]: target: The target value to be close to
@param[out]: NA
@return: ret: The value closest to target
@author: Cohen
@data: 2021/09/14
"""
def threeSumClosest(nums,target):
nums.sort()
n = len(nums)
for i in range(n):
pre = i + 1
last = n - 1
while pre < last:
temp = nums[i] + nums[pre] + nums[last]
if temp == target:
return temp
if i == 0 and pre == 1 and last == (n-1):
signalret = temp
elif (abs(temp - target) < abs(signalret - target)):
signalret = temp
if temp > target:
last -= 1
else:
pre += 1
if i == 0:
ret = signalret
elif (i > 0) and abs(signalret - target) < abs(ret - target):
ret = signalret
return ret
"""
@function: A test case
@param[in]: NA
@param[out]: NA
@return: NA
@author: Cohen
@data: 2021/09/14
"""
def test():
nums = [-1,2,1,4]
ret = threeSumClosest(nums,1)
print(ret)
def main():
test()
if __name__ == "__main__":
main()