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类似,只要维护一个变量closest作为当前和target最近的和。如果之间能够凑出来target,返回target就可以了。

有一个坑就是closest初始化的时候不能初始化为Integer.MAXVALUE,否则closest-target当target<0的时候会溢出。

比如输入 [-3,-2,-5,3,-4], -1 Integer.MAXVALUE-(-1)瞬间就溢出了。

代码如下:

 public class Solution {
public int threeSumClosest(int[] num, int target) {
if(num == null || num.length == 0)
return 0;
Arrays.sort(num);
int closest = Integer.MAX_VALUE/2; for(int i = 0;i < num.length;i++){
int start = i + 1;
int last = num.length-1;
while(start < last){
int sum = num[i] + num[start] + num[last];
if(sum == target)
return sum;
else if(sum < target)
{
start++;
}
else{
last--;
}
closest = Math.abs(closest-target) > Math.abs(sum - target)?sum:closest;
}
} return closest;
}
}

【leetcode刷题笔记】3Sum Closest的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. 【leetcode刷题笔记】3Sum

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

  6. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  7. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  8. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  9. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

随机推荐

  1. ar命令提取.o的时候报错:is a fat file (use libtool(1) or lipo(1) and ar(1) on it)

    在解压.a文件的时候,报错:s a fat file (use libtool(1) or lipo(1) and ar(1) on it),原因是该.a文件包含了多个cpu架构,比如armv7,ar ...

  2. CALayer的contentsRect

    contentsRect 想像下contentsRect作为层内容的可视区域.内容的矩形区域(x,y,width,height)也是绑定到层的框架上的.contentRect是一个比例值,而不是屏幕上 ...

  3. webserver【实时查询当天的天气情况】

    1.webserver是什么? 日常生活中经常会使用到webserver,注册时,会收到验证码,购买东西时,会收到短信,假如,A公司网站和B公司合作,那么A公司注册对的用户可以直接推送给B网站,那怎么 ...

  4. 自动化测试 python2.7 与 selenium 2 学习

    windows环境搭建 # 下载 python[python 开发环境] http://python.org/getit/ # 下载 setuptools [python 的基础包工具]setupto ...

  5. 使用程序修改系统(IE)代理设置

    文章都是发布在github再转到这边的,这边格式可能会乱掉.博客地址:benqy.com 这是本人在做的一个前端开发调试工具(HttpMock),功能是web服务器+http日记+http代理(类似f ...

  6. SpringMvc 面向切面1

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC &q ...

  7. junit测试时报No runnable methods错误的解决方法

    1.因为你@Test时import的是@org.testng.annotations.Test所以会报错 解决方法:改为import org.junit.Test;就可以了

  8. LeetCode_Minimum Depth of Binary Tree

    一.题目 Minimum Depth of Binary Tree My Submissions Given a binary tree, find its minimum depth. The mi ...

  9. 下载yum安装的rpm包

    方法一 通过yum自带的yumdownloader工具进行下载,这个工具是由yum-utils这个软件安装生成的 执行如下命令,检查yum-utils是否存在: rpm -qa |grep yum-u ...

  10. Hadoop MapReduce八大步骤以及Yarn工作原理详解

    Hadoop是市面上使用最多的大数据分布式文件存储系统和分布式处理系统, 其中分为两大块分别是hdfs和MapReduce, hdfs是分布式文件存储系统, 借鉴了Google的GFS论文. MapR ...