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. Copycat - MemberShip

    https://github.com/atomix/copycat   http://atomix.io/copycat/docs/membership/   为了便于实现,Copycat把membe ...

  2. U3d 入门

    环境搭建: 1.安装exe; 2.破解 ,百度

  3. Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类

    一.安装hibernate tools插件 1.在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3. ...

  4. Orchard Core 中数据库使用postgresql-10

    在配置好Startup.cs后,运行程序: 首先需要设置一些基本信息.这里我试着配置如下,数据库使用postgresql,连接字符串像这样: Host=192.168.0.111;UserName=u ...

  5. logback 常用配置详解(序)logback 简介

    转自:http://aub.iteye.com/blog/1101260 logback 简介 Ceki Gülcü在Java日志领域世界知名.他创造了Log4J ,这个最早的Java日志框架即便在J ...

  6. (1.6)MySQL执行计划

    关键词:mysql执行计划 1.用法 [1.1]explain select * from tab_name........ [1.2]desc select * from tab_name..... ...

  7. android打印日志封装

    public class LogUtils { static String className;//类名 static String methodName;//方法名 static int lineN ...

  8. 多线程之共享变量.md

    共享变量 - 共享变量:当多个线程同时访问一个变量的时候,会产生共享变量的问题 - 案例11 - 解决变量:锁.信号灯: - 锁(Lock): - 是一个标志,表示一个线程在占用一些资源 - 使用方法 ...

  9. 008-docker-安装-tomcat:8.5.38-jre8

    1.搜索镜像 docker search tomcat 2.拉取合适镜像 查询tags:https://hub.docker.com/ docker pull tomcat:8.5.38-jre8 d ...

  10. k8s 容器的生命周期钩子

    钩子有两个一个容器起之前定义一个动作PostStart,容器关闭之前定义一个动作PreStop 动作可以是一个命令或http请求 示例 spec: containers: - lifecycle: p ...