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. 代码高亮插件Codemirror使用方法及下载

    代码高亮插件Codemirror使用方法及下载 - 老男孩的日志 - 网易博客 代码高亮插件Codemirror使用方法及下载   2013-10-31 16:51:29|  分类: 默认分类 |   ...

  2. Java:单例模式的七种写法[转]

    第一种(懒汉,线程不安全):  1 public class Singleton {   2     private static Singleton instance;   3     privat ...

  3. c语言编程之sglib库的简单使用

    说实话自从大学毕业后已经很久没有用c语言写过程序了,一般都是使用c++,c++的stl和boost等,这些代码库大大简化了我们的编程复杂度.由于最近某种原因在次开始用c写程序.我是个比较懒的人,比较喜 ...

  4. C# winform带进度条的图片下载

    代码如下: public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void ...

  5. git删除未跟踪文件

    # 删除 untracked files git clean -f   # 连 untracked 的目录也一起删掉 git clean -fd   # 连 gitignore 的untrack 文件 ...

  6. android调试系列--使用ida pro调试原生程序

    1.工具介绍 IDA pro: 反汇编神器,可静态分析和动态调试. 模拟机或者真机:运行要调试的程序. 样本:自己编写NDK demo程序进行调试 2.前期准备 2.1  准备样本程序(假设已经配置好 ...

  7. MVC笔记

    简要论述对MVC模式的理解,并简述ThinkPHP中的MVC模式是如何运行的 MVC(Model-View-Controller)应用程序结构被用来分析分布式应用程序的特征.这种抽象结构能有助于将应用 ...

  8. ASP.NET优化性能方法之一禁用调试模式(转)

    若要设置 ASP.NET 应用程序的调试模式,必须编辑应用程序的 Web.config 配置文件. 通常,ASP.NET 应用程序的 Web.config 文件与应用程序位于相同的 URL 位置上. ...

  9. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  10. CocoaPod安装

    http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml http://www.bubuko.com/infodetail-4 ...