题意

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. quarz时间配置

    Cron表达式的格式:秒 分 时 日 月 周 年(可选). 字段名                 允许的值                            允许的特殊字符     秒     ...

  2. 为MySQ启用HugePage

    8.12.4.2 Enabling Large Page Support Some hardware/operating system architectures support memory pag ...

  3. Java 基本数据类型 && 位运算

    1. Java基本数据类型 1.1 数据类型示意图 类型 字节数 范围 byte 1 -128~127 short 2 -32768~32767 int 4 -231~231-1 long 8 -26 ...

  4. 【转】Java学习---线程间的通信

    [原文]https://www.toutiao.com/i6572378564534993415/ 两个线程间的通信 这是我们之前的线程. 执行效果:谁抢到资源,谁运行~ 实现线程交替执行: 这里主要 ...

  5. C#程序如何捕捉未try/catch的异常——不弹“XXX已停止工作”报错框

    诚意满满直接上代码: static void Main(string[] args) { //Main函数中增加此句 AppDomain.CurrentDomain.UnhandledExceptio ...

  6. MySQL基础之 排序与限制,聚合

    排序与限制 ORDER BY 作用:取出按照某个字段进行排序后的记录结果集. 配合:常与DESC  和ASC一块使用:默认是ASC,表示升序.DESC表示降序 LIMIT 作用:用于显示数据的一部分记 ...

  7. 使用Socket开发http服务器时碰到的问题及处理方法

    1. 前言 ​ 最近正在为QA测试开发压力测试框架,要为测试人员提供一个结果的图形化表示界面.为了展示数据的及时性,不得不使用lua语言实现一个http服务器.由于http服务需要提供的服务比较简单 ...

  8. python第三十六课——1.可迭代对象

    1.可迭代对象: 满足前提: 只要能被循环操作的对象,就可以可迭代对象 举例: str.list.tuple.set.dict.range.generator... 高效的检测一个对象是否是可迭代对象 ...

  9. 矩阵dp

    矩阵dp 这里是矩阵dp,不是矩阵乘法优化dp. 矩阵上的dp好像也没什么特殊的?大概有一个套路就是从上向下,从左向右进行dp吧. 首先第一道题好像不是矩阵dp... 1005 矩阵取数游戏:http ...

  10. 由于没有公钥,无法验证下列签名: NO_PUBKEY 54422A4B98AB5139

    gpg --keyserver pgpkeys.mit.edu --recv-key 54422A4B98AB5139 gpg -a --export 54422A4B98AB5139 | sudo ...