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. SOA 新业务语言 新系统架构——什么是SOA

    原文地址:http://blog.csdn.net/ichaos/archive/2008/01/20/2054377.aspx SOA的概念是Gartner在1996年提出来的,并于2002年12月 ...

  2. Hash Map (Hash Table)

    Reference: Wiki  PrincetonAlgorithm What is Hash Table Hash table (hash map) is a data structure use ...

  3. hdu 5424 Rikka with Graph II(dfs+哈密顿路径)

    Problem Description   As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...

  4. hdu 5178 pairs (线性探查问题)

    Problem Description John has n points on the X axis, and their coordinates are (x[i],),(i=,,,…,n−). ...

  5. CUGBACM_Summer_Tranning 组队赛解题报告

    组队赛解题报告: CUGBACM_Summer_Tranning 6:组队赛第六场 CUGBACM_Summer_Tranning 5:组队赛第五场 CUGBACM_Summer_Tranning 4 ...

  6. IOS开发之格式化日期时间

    IOS开发之格式化日期时间(转)   在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如: //实例化一个NSDateFor ...

  7. XMLHttpRequest取得响应

    RresponseText:获得字符串形式的响应数据 responseXML:获得XML形式的响应数据 status和statusText:以数字和文本形式返回HTTP状态码 getAllRespon ...

  8. URLScan安装及配置(转)

    安装 URLScan 要安装 URLScan,请访问下面的 Microsoft Developer Network (MSDN) 网站: http://msdn2.microsoft.com/en-u ...

  9. Jquery:Jquery中的DOM操作<二>

    由于昨天晚上回来的晚,写的有点匆忙,所以昨天的学习笔记中出现了多处错误的地方,幸好有各位园友帮忙指出,在这里谢过各位了!今天继续学习关于Jquery中DOM的操作,其实是昨天随笔的延续,求围观!!! ...

  10. sql server windows账号不能登陆指定的数据库

    问题描述: 1. windows账号登陆后,默认的数据库被删除 2. SA账号密码也忘记了 此时就会导致用windows账号登陆Sql Server的时候,返回4064的错误,按照上面的问题描述,应该 ...