16. 3Sum Closest

  • Total Accepted: 86565
  • Total Submissions: 291260
  • Difficulty: Medium

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

 思路:

  本题的解法和上一次3Sum的解法有点类似,我们可以先将数组排序,然后将数组中的数从左到右依次确定为第一个数,在其右边的数中寻找最接近的target的数即可。重要的是判断逻辑的思路。代码如下:

 public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return 0;
} Arrays.sort(nums); int closest = nums[0]+nums[1]+nums[2] ; for(int i = 0 ; i < nums.length-2 ; i++){
int l = i+1 ;
int r = nums.length-1 ;
while(l < r){
int sum = nums[i] + nums[l] + nums[r] ;
if(sum == target){
return sum ;
}else if(sum < target){
/*
* 三种情况:
* 1、closest < sum < target --->closest = sum
* 2、sum < target < closest --->| target-sum < closest-target ---> closest = sum
* | target-sum >= closest-target --> closest不变
* 3、sum <= closest <= target ---> closest不变
*/
if(sum > closest){
//情况1
closest = sum ;
}else if(closest > target){
//情况2
if(target-sum < closest-target){
//情况2.1,
closest = sum ;
}
//情况2.2不用变化
}
//情况3不同变化
l++ ;
}else{
//分析同上
if(sum < closest){
closest = sum ;
}else if(closest < target){
if(sum-target <= target-closest){
closest = sum ;
}
}
r-- ;
}
} }
return closest ;
}

No.016 3Sum Closest的更多相关文章

  1. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  2. 【JAVA、C++】LeetCode 016 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][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  4. 016 3Sum Closest 最接近的三数之和

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

  5. 【LeetCode】016 3Sum Closest

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

  6. [Leetcode]016. 3Sum Closest

    public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...

  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. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  9. 【leetcode】3Sum Closest

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

随机推荐

  1. c#基础知识-2

    1.在控制台接受数据时可以这样输入: using System; using System.Collections.Generic; using System.Linq; using System.T ...

  2. Css中常用中文字体的Unicode编码对照

    在网页制作中,最常用的恐怕是字体属性了,在调整页面兼容的时候,也常常发现字体名称的原因导致不兼容或乱码,下面给出几种常用字体的ucicode编码对照,方便使用. 宋体 SimSun \5B8B\4F5 ...

  3. 人脸识别SDK小结

    Face++人脸识别 进入官网 Face++ 致力于研发世界最好的人脸技术,提供免费的API和SDK供企业和开发者调用,更有灵活的定制化服务满足不同需求.已有多家公司使用Face++技术服务,完成包括 ...

  4. 将从数组中取到的字符串赋值给了UIImage导致的错误

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantStr ...

  5. UIkit框架之UIDatePicker

    1.继承链:UIcontrol:UIview:UIResponder:NSOobject 2.和uidatepicker相关联的触发事件是 UIControlEventValueChanged,当使用 ...

  6. linux命令:locate

    1.命令介绍: locate用来查找文件,它是在系统的数据库中查找,所以速度非常快. 2.命令格式: locate [选项] 模式         ---这里的模式是指正则表达式 3.命令参数: -e ...

  7. PHP 调试

    $log_path = '/data0/www/html/hejungw/static/log/uploadlog/'; $log = $log_path . date('Y-m-d') . '.lo ...

  8. JSP EL表达式 与输入有关的内置对象

    与输入有关的隐含对象有两个param和paramValues,用来取得用户的请求参数.在使用EL之前可以使用如下代码取得用户的请求参数: request.getParameter(String nam ...

  9. JS 点击复制Copy (share)

    分享自:http://www.cnblogs.com/athens/archive/2013/01/16/2862981.html 1.实现点击按钮,复制文本框中的的内容 1 <script t ...

  10. iOS下UITableView的单元格重用逻辑

    终于有时间继续UITableView的接口调用顺序这篇文章了,之前测试过,模拟器都是按照height,cellForRow这样的顺序调用接口的,iOS8以前一直是这样,但是到了iOS8,这个顺序倒过来 ...