Question

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.

Example

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

Solution

Similar with 3 Sum. Time complexity is O(n2)

 public class Solution {
public int threeSumClosest(int[] numbers ,int target) {
int length = numbers.length;
Arrays.sort(numbers);
int min = Integer.MAX_VALUE;
int result = numbers[0] + numbers[1] + numbers[2];
for (int i = 0; i < length - 2; i++) {
if (i > 0 && numbers[i] == numbers[i - 1])
continue;
int l = i + 1, r = length - 1;
while (l < r) {
while (l < r && numbers[l] == numbers[l + 1])
l++;
while (l < r && numbers[r] == numbers[r - 1])
r--;
int sum = numbers[i] + numbers[l] + numbers[r];
if (sum > target)
r--;
else if (sum < target)
l++;
else
return target;
int tmp = Math.abs(sum - target);
if (tmp < min) {
min = tmp;
result = sum;
}
}
}
return result;
}
}

3 Sum Closest 解答的更多相关文章

  1. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  2. Subarray Sum Closest

    Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...

  3. 3Sum Closest 解答

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

  4. [leetcode]3 Sum closest

    问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  5. 3 sum closest

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

  6. Combination Sum II 解答

    Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...

  7. Path Sum II 解答

    Question Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the ...

  8. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  9. (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数

    原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...

随机推荐

  1. 【剑指offer】面试题44:扑克牌的顺子

    题目: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定 ...

  2. 关于matlab中textread

    本文主要内容引自http://linux.chinaitlab.com/administer/872894.html 笔者在此基础上进行运行,修改得到以下内容,希望大家给与补充: textread 基 ...

  3. pager-taglib 使用说明2

    传两个值进去:1.pm.totles总记录数 2.pagesize 每页显示页数 3.<pg:param name="parentId"/>传给后台的变量值对(查询条件 ...

  4. 工作中用到的linux命令

    都是工作中用到的,解决问题至上,不求甚解,怕再忘了,所以记录一下,勿喷. .log |,,,,|,| 先说一下这条命令: cat:打印文件内容 grep:查找,用到的有\s匹配空白字符 sed:刚用到 ...

  5. pyqt之倒计时例子

    from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main():    a= ...

  6. 初入Python继承

    1.什么是继承? 新类不用从头编写 新类从现有的类继承,就自动拥有了现有类的所有功能 新类只需要编写现有类缺少的新功能 2.继承的好处 复用已有代码 自动拥有了现有类的所有功能 只需要编写缺少的新功能 ...

  7. ipython with ubuntu

    在Linux环境下,其实IDE环境配置比较容易配.所以建议用linux做开发. 首选启动终端:Ctrl+Alt+T sudo apt-get update sudo apt-get install p ...

  8. hdu4126Genghis Khan the ConquerorGenghis Khan the Conqueror(MST+树形DP)

    题目请戳这里 题目大意:给n个点,m条边,每条边权值c,现在要使这n个点连通.现在已知某条边要发生突变,再给q个三元组,每个三元组(a,b,c),(a,b)表示图中可能发生突变的边,该边一定是图中的边 ...

  9. Swift 2.0 封装图片折叠效果

    文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...

  10. LoadRuner性能测试之内存分析方法及步骤(Windows)

    1.首先观察Available  Mbytes(可用内存),至少要>=1/2的内存空间 2.然后观察Pages/sec值是不是很大 3.再观察Page  Faules/sec是不是很大,其值表示 ...