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. Active Record: 資料庫遷移(Migration) (转)

    Active Record: 資料庫遷移(Migration) Programming today is a race between software engineers striving to b ...

  2. 算法(第4版)-1.3.1 API

    总结:本小节介绍了泛型.自动装箱.迭代.Bag.Queue.Stack以及一个栈用例的经典例子--算术表达式求值. 重点: 1. 集合类的抽象数据类型的一个关键特性是我们应该可以用它们储存任意类型的数 ...

  3. 跟大牛之间关于hibernate的一些探讨记录

    hibernate的工作原理!! 1.读取配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transcation 5.持久化操作 6.提交事务 ...

  4. watir学习系列--Watir API介绍

    文本框:          <INPUT id="email" name="_fmu.u._0.e" value="" />   ...

  5. CI框架引入外部css和js文件

    首先在项目根目录下建立assets文件夹,在这个文件夹下再建立css和js文件夹分别放置css和js文件 然后,在项目根目录下建立.htaccess文件 内容如下: RewriteEngine on  ...

  6. source insight 相对路径

    source insight项目 在移动到另外一个地方时,会因为之前是绝对路径而导致,项目中的文件都不可用,需要重新把这些文件添加一遍. 这是个令人讨厌的事情. 解决办法为创建项目时设定为绝对路径.方 ...

  7. CSS颜色代码大全

    CSS颜色代码大全 转载:http://blog.163.com/wujinhongisme@126/blog/static/3613698020095115919389/ RGB ( Red,Gre ...

  8. 在线聊天室的实现(2)--基于Netty 4.x的Echo服务器实现

    前言: 就如前文所讲述的, 聊天室往往是最基本的网络编程的学习案例. 本文以WebSocket为底层协议, 实现一个简单的基于web客户端的Echo服务. 服务器采用Netty 4.x来实现, 源于其 ...

  9. Java问题总结

    1.如何查看使用java的版本 cmd-->java -version 2.如何下载jdk,sdk Java.JDK(工具包)的安装_百度经验http://jingyan.baidu.com/a ...

  10. Python笔记 001

    #python版本:3.5.2 #for循环 for letter in ("xuyingke"): #默认循环 print ("当前字母:",letter) ...