一.题目链接:https://leetcode.com/problems/3sum-closest/

二.题目大意:

 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的和最接近target。

三.题解:

这道题实质就是3sum(http://www.cnblogs.com/wangkundentisy/p/9079622.html)问题的变形,而且题目假设的是解是唯一的,所以该题甚至都不用考虑重复情况了。所以只需在3sum问题中,判断下三个元素的和与target的差值,并根据差值进行赋值即可。

代码如下:

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
int differ = 0x7fffffff;//初始化差值
int sum = 0;
if(len < 3)
return 0;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i -1])
continue;
int j = i + 1, k = len - 1;
while(j < k)
{
if(nums[i]+ nums[j] + nums[k] == target)
{
return target;//由于题目假设的是解释唯一的,所以如果遇到正好和为target的情况,可以立马返回
}
else if(nums[i] + nums[j] + nums[k] < target)
{
int temp = target - (nums[i] + nums[j] + nums[k]);
if(temp < differ)//找到离target差异最小的一组数据
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
j++;
}
else
{
int temp = (nums[i] + nums[j] + nums[k]) - target;
if(temp < differ)
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
k--;
}
}
}
return sum;
}
};

由于本题基本与3sum问题一致,所以注意事项直接看3sum问题就行了。

LeetCode——16. 3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  6. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. [LeetCode] 16. 3Sum Closest ☆☆☆

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. Unity射线检测的用法总结

    RayCast 射线检测 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...

  2. Atcode ABC105-C:Base -2 Number

    C - Base -2 Number Time Limit: 2 sec / Memory Limit: 1024 MB Score : 300300 points Problem Statement ...

  3. centos7 启动mysql

    密码无法登录问题: 在my.cnf  中加一句   skip-grant-tables : 重启mysql服务: mysql -uroot -p: USE mysql ; 进入后,修改密码 .UPDA ...

  4. Ubuntu18.10下运行blender2.80bate闪退(问题?)

    Ubuntu18.10下直接运行blender2.80bate闪退, 运行blender2.79正常. ================= root@tom-laptop:/# uname -aLin ...

  5. music cube

    music cubehttps://www.youtube.com/watch?v=HBCdC7r7Mp4Blender Tutorial: Make anything react with musi ...

  6. python django day 3 页面,自动 跳转,参数传递

    zqxt_views/urls.pypath('', calc_views.index, name='home'), calc/views.pydef index(request): return r ...

  7. 【SpringCloud】初识springCloud

    转载来源:https://maimai.cn/article/detail?fid=1149221357&efid=lL_Z3WzTyUDLAjQwrNrIsw 概述 毫无疑问,Spring ...

  8. POJ2480 Longge's problem

    题意 Language:Default Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1064 ...

  9. golang fatal error: concurrent map read and map write

    调试程序的时候,为了打印map中的内容 ,直接 使用seelog 的方法打印 map中的内容到日志,结果出现 “concurrent map read and map write”的错误,导致程序异常 ...

  10. centos7升级Python版本后,yum不能正常使用

    python升级方法,使用源码编译安装即可,prefix=/usr/local/bin/python3 执行yum list,提示/usr/bin/yum 报错 我是直接在Python2.7的基础上又 ...