一.题目链接: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. virtualenv搭建python3 环境

    参考 1.安装python3 安装脚本如下: wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz tar zxvf Python ...

  2. 运放积分电路MULTISIM

    有些需要反馈回路

  3. 利用sql语句进行增删改查

    1.查询 函数:raw(sql语句) 语法:Entry.objects.raw(sql) 返回:QuerySet 2.增删改 from django.db import connection def ...

  4. unet网络讲解,附代码

    转: http://www.cnblogs.com/gujianhan/p/6030639.html key1: FCN对图像进行像素级的分类,从而解决了语义级别的图像分割(semantic segm ...

  5. python 闭包和迭代器

    一  函数名的运用:(函数名是一个变量,但它是一个特殊变量,与括号配合可以执行变量. (1) 函数名可以赋值给其他变量 def chi(): print("吃月饼") fn=chi ...

  6. 2017.7.11 linux 挂载

    挂载:Liunx采用树形的文件管理系统,也就是在Linux系统中,可以说已经没有分区的概念了.分区在Linux和其他设备一样都只是一个文件.要使用一个分区必须把它加载到文件系统中.这可能难于理解,继续 ...

  7. djjango cookie和session 的几种常用需求使用方法

    ------https://www.cnblogs.com/liuqingzheng/articles/8990027.html 需求情形一:正常设置cookie set_cookie(key,val ...

  8. C++学习(八)(C语言部分)之 图形库

    有关图形库的学习笔记 1.安装 ww.easys.cn 2.创建win32控制台应用程序 .cpp文件(图形库必须创建cpp文件) *重点 3.安装好后 重启一下vs 图形库 是一些函数的集合 作用是 ...

  9. Go Example--通道非阻塞

    package main import ( "fmt" ) func main() { messages := make(chan string) signals := make( ...

  10. day11hadoop高可用和Hive

    PS:视频一直就是在演示   高可用(比较偏运维一点) PS:Active是对外提供服务的,standBy是从属备用的:但是他们是怎样保证同步的数据的呢?一个运行中zookeeper上的第三方那个工具 ...