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. 马士兵 Servlet_JSP(3) Servlet和JSP的通信(源代码)

    (1)从JSP调用Servlet可用<jsp:forward>,请求信息自动转到Servlet FromJspToServlet.jsp <html>     <body ...

  2. QT实现透明效果的按钮

    QPushButton { color: rgb(0,88,152) background-color: rgba(97%,80%,9%,50%)}

  3. Linux多任务编程——线程

    线程基础 △ 由于进程的地址空间是私有的,因此在进行上下文切换时,系统开销比较大 △ 在同一个进程中创建的线程共享该进程的地址空间 △ 通常线程值得是共享相同地址空间的多个任务 △ 每个线程的私有这些 ...

  4. ncsim仿真VHDL

    ncsim仿真VHDL 1.文件列表 ctrl.vhd design_io.vhd tb.vhd compile.nc simulate.nc ./shm/shmtb.tcl 2. Compile你的 ...

  5. 【转】iOS开发24:使用SQLite3存储和读取数据

    转自:http://my.oschina.net/plumsoft/blog/57626 SQLite3是嵌入在iOS中的关系型数据库,对于存储大规模的数据很有效.SQLite3使得不必将每个对象都加 ...

  6. [转]CSS目标伪类E:target

    CSS3 target 伪类不得不说那些事儿(纯CSS实现tab切换) 是不是觉得target有点眼熟?! 今天要讲的不是HTML的<a>标签里面有个target属性. target伪类是 ...

  7. java Math.random()随机数的产生

    Math.random()是java内置产生随机数的函数,Math.random()能够产生[0,1)的浮点数,当我们要产生特定范围的数时,可以采用如下办法: 1.Math.random()*(最大数 ...

  8. visifire 图表双坐标轴 silverlight

    public void CreateChart(Grid oGrid, ObservableCollection<ListItem> lBaseOilBar)        {       ...

  9. Jquery常用方法(转)

    原文:http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库.据 ...

  10. Spring dbcp连接池简单配置 示例

    一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...