题目:

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. linux安装及配置c++的opencv库

    linux安装及配置c++的opencv库 前言: 最近想搞个机器视觉的比赛,要求是linux+opencv环境,没有做过opencv开发的我配置环境就配了两天,看来很多乱七八糟的博客,终于装好了.网 ...

  2. NCPC 2016 October 8,2016 Artwork

    Problem A Artwork Problem ID: artwork Time limit: 4 seconds A template for an artwork is a white gri ...

  3. MCI:移动持续集成在大众点评的实践

    一.背景 美团是全球最大的互联网+生活服务平台,为3.2亿活跃用户和500多万的优质商户提供一个连接线上与线下的电子商务服务.秉承“帮大家吃得更好,生活更好”的使命,我们的业务覆盖了超过200个品类和 ...

  4. 转 SSM框架整合to萌新

    作用: SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,DAO层四层 使用spring ...

  5. [leetcode DP]53. Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. [ 转载 ] Java基础14--创建线程的两个方法

    http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类 ...

  7. 2809: [Apio2012]dispatching 可并堆 左偏树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2809 板子题wa了一下因为输出ans没有lld #include<iostream> ...

  8. 【洛谷】2474:[SCOI2008]天平【差分约束系统】

    P2474 [SCOI2008]天平 题目背景 2008四川NOI省选 题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A ...

  9. python数据库操作——sqlite3模块

    # -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...

  10. SGU 403 Scientific Problem

    403. Scientific Problem Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: stan ...