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

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution {
public:
int threeSumClosest(vector<int>& a, int target) {
const int n = a.size();
int res = ;
if (n < ) {
for (auto& ii :a){
res+=ii;
}
return res;
}
std::sort(a.begin(),a.end());
res = a[] + a[] + a[];
for(int i = ; i < n-; ++i) {
int low = i + ;
int high = n - ;
while(low < high) {
int cur_sum = a[i] + a[low] + a[high];
if (abs(cur_sum - target) < abs(res - target)) {
res = cur_sum;
};
if (cur_sum > target) {
high--;
} else if (cur_sum < target) {
low++;
} else {
return target;
}
}
}
return res; }
};
 class Solution {
public int threeSumClosest(int[] nums, int target) {
int res = nums[0]+nums[1]+nums[2];
Arrays.sort(nums);
for(int i = 0 ; i<nums.length-2;i++){
int lo=i+1,hi=nums.length-1;
while(lo<hi){
int sum=nums[i]+nums[lo]+nums[hi];
if(sum>target) hi--;
else lo++;
if(Math.abs(sum-target)<Math.abs(res-target))
res = sum;
}
}
return res;
}
}

16. 3Sum Closest(双指针)的更多相关文章

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

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

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  3. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

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

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

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

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

  7. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

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

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

  9. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

随机推荐

  1. json解析出来数据为空解决方法

    从APP端或从其他页面post,get过来的数据一般因为数组形式.因为数组形式不易传输,所以一般都会转json后再发送.本以为发送方json_encode(),接收方json_decode(),就解决 ...

  2. hadoop 学习笔记2

    ============Hive vs Hadoop============== Hive是建立在Hadoop之上为了减少MapReduce jobs编写工作的批处理系统,HBase是为了支持弥补Ha ...

  3. 图->连通性->无向图的连通分量和生成树

    文字描述 对无向图进行遍历时,对于连通图,仅需从图中任一顶点出发,进行深度优先搜索或广度优先搜索,便可访问到图中所有顶点.但对非连通图,则需从多个顶点出发搜索,每一次从一个新的起始点出发进行搜索过程得 ...

  4. Word Embedding理解

    一直以来感觉好多地方都吧Word Embedding和word2vec混起来一起说,所以导致对这俩的区别不是很清楚. 其实简单说来就是word embedding包含了word2vec,word2ve ...

  5. ubuntu编译opencv3.1遇到的问题

    网上有很多关于编译的具体步骤,我也是按照网上的说明一步步操作的,这里主要想记录的是在安装完之后,import cv2不存在以及其他的libopencv_hdf.so.3.1等找不到的问题,如果将这样的 ...

  6. what's the 爬虫之基本原理

    what's the 爬虫? 了解爬虫之前,我们首先要知道什么是互联网 1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,总体上像一张网一样. 2.互联 ...

  7. 009-ThreadPoolExecutor运转机制详解,线程池使用1-newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool

    一.ThreadPoolExecutor理解 为什么要用线程池: 1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务. 2.可以根据系统的承受能力,调整线程池中工作线线程的数 ...

  8. 【C++问题整理】

    一.static   1.作用:    静态变量/函数:在整个文件内可见,不会被其他文件所用:静态变量:会被自动初始化为0: 类中的静态变量:类的成员,类对象公用 类中的静态函数:只能访问静态变量 2 ...

  9. PHP 判断括号是否闭合

    一开始的思路就是判断每种括号的开闭数量是否相等,其实虽然也能实现但是搞得太复杂了: 后来查了查,只需设一个常量,左括号  +1,右括号   -1,闭合的话为0,没闭合的话不为0, 出现<0即为顺 ...

  10. (转)MySQL排序原理与案例分析

    前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct ...