题意

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

给定一个数组及一个目标值,找出三个数,使它们的和与目标值的差值最小,输出它们的和。

解法

与上一题3Sum一样,遍历第一个数,然后用双指针算法遍历剩下两个数,随时比较它们与目标值的差值。

class Solution
{
public:
int threeSumClosest(vector<int>& nums, int target)
{
sort(nums.begin(),nums.end()); int diff = 0x7fffffff;
int ans = 0; for(int i = 0;i < nums.size();i ++)
{
int j = i + 1;
int k = nums.size() - 1; while(j < k)
{
int sum = nums[i] + nums[j] + nums[k];
if(abs(sum - target) < diff)
{
diff = abs(sum - target);
ans = sum;
} if(sum < target)
j ++;
else
k --;
}
}
return ans;
}
};

LeetCode 3Sum Closest (Two pointers)的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

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

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

  3. Leetcode 3Sum Closest

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

  4. LeetCode——3Sum Closest

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

  5. leetcode 3Sum Closest python

    class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  7. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  8. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. 【SPL标准库专题(9)】 Datastructures:SplObjectStorage

    PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...

  2. jbosscache

    JBossCache 讲解说明 是什么? 一个树形结构.支持集群.支持事务的缓存技术. 有什么作用? JBoss Cache是针对Java应用的企业级集群解决方案,其目的是通过缓存需要频繁访问的Jav ...

  3. 【转】Java学习---内存泄露与溢出的区别

    Java内存泄露与溢出的区别 Java内存泄漏就是没有及时清理内存垃圾,导致系统无法再给你提供内存资源(内存资源耗尽): 而Java内存溢出就是你要求分配的内存超出了系统能给你的,系统不能满足需求,于 ...

  4. a.c:5:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wun

    PTA做题时出现的错误,用if括起来就没有了. if(scanf("%d",&a)){}; 其实并不是这里有问题,如果你的输出有问题,他就会鸡蛋里挑骨头的先显示这个错误.

  5. Linux命令一览

    Linux系统中的命令参数有长短格式之分,长格式和长格式之间不能合并,长格式和短格式之间也不能合并,但短格式和短格式之间是可以合并的,合并后仅保留一个-(减号)即可. echo命令:用于在终端输出字符 ...

  6. 邮局加强版:四边形不等式优化DP

    题目描述 一些村庄建在一条笔直的高速公路边上,我们用一条坐标轴来描述这条公路,每个村庄的坐标都是整数,没有两个村庄的坐标相同.两个村庄的距离定义为坐标之差的绝对值.我们需要在某些村庄建立邮局.使每个村 ...

  7. 【转】PHP解析带CDATA的XML方法

    XML文件,如下 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName& ...

  8. PHP错误提示的关闭方法详解

    关闭PHP错误脚本提示是程序上线了必须做的一件事情,就是不管程序怎么报错我们都不能让错误日志在服务器上给大家看到,下面我来总结两种关闭PHP错误脚本提示的具体方法     最简单的办法就是直接在php ...

  9. HTML5调用百度地图API获取当前位置并直接导航目的地的方法

    <!DOCTYPE html> <html lang="zh-cmn-Hans">     <meta charset="UTF-8&quo ...

  10. linux 设置默认网关永久

    .永久添加 1 2 vim /etc/sysconfig/network GATEWAY=192.168.1.4