题目:

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的变体,这里找到的不仅使3sum==target,同时如果没有找到==target的3sum要返回最接近target的值。
于是,这就需要在使用二分查找法时遍历数组的时候,维护一个最接近target值min,这样当完全遍历完数组扔没找到与target相等的3sum时,可以返回维护的这个min值。
这道题比3sum和4sum简单的地方就是不需要判断重复问题,因为题目给我们减轻了去重的压力,have exactly one solution。
即便要求去重,使用之前说过的两个方法:HashSet和挪动指针法,也可以很容易就去重了。
这里要注意,判断closest的方法是采取target-sum的绝对值与min相比,很容易理解,无论这个closest是在target左还是右,离target最近的就是最closest的。 实现代码如下:
 1 public int threeSumClosest(int[] num, int target) {
 2             if(num==null || num.length<3)
 3                 return 0;
 4             
 5             int min = Integer.MAX_VALUE;
 6             int val = 0;
 7             Arrays.sort(num);
 8             for(int i = 0; i<=num.length-3;i++){
 9                    int low = i+1;
                    int high = num.length-1;
                    while(low<high){
                         int sum = num[i]+num[low]+num[high];
                         if(Math.abs(target-sum)<min){
                             min = Math.abs(target-sum);
                             val = sum;
                         }
                         
                         if(target==sum){
                             return val;
                         }else if(target>sum){
                             low++;
                         }else{
                             high--;
                         }
                    }
             }
             return val;
       }
												

3Sum Closest leetcode java的更多相关文章

  1. 3Sum Closest - LeetCode

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

  2. 3Sum Closest——LeetCode

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

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

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

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

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

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

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

  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. Object-c和Java中的代理

    代理模式的核心思想就是一个类想做一件事情(函数),但它不去亲自做(实现),而是通过委托别的类(代理类)来完成. 代理模式的优点: 1.可以实现传值,尤其是当一个对象无法直接获取到另一个对象的指针,又希 ...

  2. MIT-6.828-JOS-环境搭建

    MIT 6.828是操作系统中最经典的一门课程.完成所有的lab就相当于完成了一个迷你的操作系统.我跟的是2018年的课程,课程首页在6.828课程官网.当然所有资料都是英文的,所以难度也不低,这里推 ...

  3. leetcode 奇偶链表 python

    要求空间复杂度O(1) 那就只能用指针不断改链表的指针, 不能建立新的内存 时间复杂度O(1) 一遍遍历 不能嵌套循环 我的思想是: 1 如果链表元素数量小于等于2个,那就无法操作 2 能操作的情况下 ...

  4. php异步实现,避免长时间等待

    处理的php异步的方法有好几种,这里我就只介绍我经常用的而且官方也推荐的 废话少说,直接贴代码 //php异步 public function doRequest($host,$path, $para ...

  5. 【贪心】【后缀自动机】Gym - 101466E - Text Editor

    题意:给你两个串A,B,以及一个整数K,让你找到B的一个尽可能长的前缀,使得其在A串中出现的次数不小于K次. 对A串建立后缀自动机,然后把B串放在上面跑,由于每到一个结点,该结点endpos集合的大小 ...

  6. 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境

    Bash shell(四)-Bash shell的使用环境   是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...

  7. poj 1825 Ants 水题

    Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10722   Accepted: 4752 Description ...

  8. php模块组成

    php总共有三个模块:内核.ZEND引擎.扩展. 内核是用来处理请求.文件流.错误处理等操作的: ZEND引擎是将源文件转换成机器语言,然后在虚拟机上运行: 扩展层是一组函数.类库和流,php使用它们 ...

  9. 【原】Order属性决定了不同切面类中通知执行的先后顺序

    [障碍再现] MyBatis配置多数据源时,数据源切换失败. [原因分析]    自定义切面和Spring自带事务切面“即<aop:advisor>”执行的先后顺序导致数据源不能切换成功. ...

  10. Node.js学习笔记(3) - 简单的curd

    这个算是不算完结的完结吧,前段时间也是看了好久的Node相关的东西,总想着去整理一下,可是当时也没有时间: 现在看来在整理的话,就有些混乱,自己也懒了,就没在整理,只是简单的记录一下 一.demo的简 ...