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.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

题意就是给定一个数组,一个target,在数组中寻找一个三元组之和最接近target的值,这个题是之前3Sum的变形,不同的是这个题没有重复元素,而且设定解唯一。

我的思路是:跟之前的3sum一样,保留左右各一个pointer,遍历的时候,有left和right两个游标,left就从i+1开始,right就从数组最右length-1开始,如果i、left、right三个元素加起来等target,那么就可以加入结果的List中了,如果sum-target>0,那么说明sum比较大,right应该向左移动,反之left向右移动。

Talk is cheap>>

   public int threeSumClosest(int[] num, int target) {
if (num == null || num.length < 3) {
return 0;
}
int min = Integer.MAX_VALUE;
int res=0;
Arrays.sort(num);
for (int i = 0; i < num.length - 2; i++) {
int left = i + 1;
int right = num.length - 1;
while (left < right) {
int sum = num[i] + num[left] + num[right];
// System.out.printf(i+" "+sum);
if (sum == target) {
return target;
}
if (min>=abs(sum-target)){
min = abs(sum-target);
res=sum;
}
if (sum-target>0){
right--;
}else {
left++;
}
}
}
return res;
}
public int abs(int a){
return Math.abs(a);
}

3Sum Closest——LeetCode的更多相关文章

  1. 3Sum Closest - LeetCode

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

  2. 3Sum Closest leetcode java

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

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

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

  4. 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 ...

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

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

  6. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  7. LeetCode:3Sum, 3Sum Closest, 4Sum

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

  8. 【leetcode】3Sum Closest

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

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

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

随机推荐

  1. rabbitmq Clustering Guide--官方

    官方文档地址:http://www.rabbitmq.com/documentation.html A RabbitMQ broker is a logical grouping of one or ...

  2. [转] linux新的API signalfd、timerfd、eventfd使用说明

    http://blog.csdn.net/gdutliuyun827/article/details/8460417 三种新的fd加入linux内核的的版本: signalfd:2.6.22 time ...

  3. iOS开发系列之远程控制事件

    在今天的文章中还剩下最后一类事件:远程控制,远程控制事件这里主要说的就是耳机线控操作.在前面的事件列表中,大家可以看到在iOS中和远程控制事件有关的只有一个- (void)remoteControlR ...

  4. 【转】string常用函数

    原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...

  5. Jquery分步学习一

    <script type="text/javascript" src="../js/jquery-1.11.0.js"></script> ...

  6. Linq101-QueryExecution

    using System; using System.Linq; namespace Linq101 { class QueryExecution { /// <summary> /// ...

  7. WebSocket协议

    websocket 简介 (2013-04-09 15:39:28) 转载▼   分类: websocket 一 WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例 ...

  8. 【转】 iOS使用AVFoundation实现二维码扫描

    原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...

  9. C语言的运行机制

    目的:通过分析c语言转换成汇编代码后的执行过程对汇编语言和X86构架有一个初步认识 实验代码 1 #include <stdio.h> 2 3 int g(int x) 4 { 5 ret ...

  10. java常识和好玩的注释

    如字符串使用strXXXboolean使用isXXX,hasXXX Vector vProducts= new Vector(); Array aryUsers= new Array(); 类与接口基 ...