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

Solution

和3Sum的思路基本一样。也是固定起点后,双指针遍历。

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

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

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

  2. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  3. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  5. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  6. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

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

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

随机推荐

  1. (Stack)Basic Calculator I && II

    Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...

  2. hdu2089:不要62(基础数位dp)

    题意:规定一个合法的号码不能含有4或者是连续的62 给定区间[n,m] 问此区间内合法的号码的个数 分析:数位dp dp[i][j]代表 最高位为 j 的 i 位数有多少个合法的 然后按题目规则进行转 ...

  3. Android单元测试: 首先,从是什么开始

    Android单元测试: 首先,从是什么开始 http://chriszou.com/2016/04/13/android-unit-testing-start-from-what.html 这是一系 ...

  4. warning: the `gets' function is dangerous and should not be used.(转)

    今天在LINUX下编译C程序时,出现了:warning: the `gets' function is dangerous and should not be used. 这个warning. 百度之 ...

  5. poj 3666 Making the Grade(dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  6. 如何在android上去控制开发进度

    这次android的壁纸软件1.0版本终于可以上线了,软件的功能基本上实现了,但是用户体验不太好.在整个开发阶段和测试阶段,出现了很多预料之外的事情,比如size是1M多的json文件解析.高清图片导 ...

  7. hdu2531之BFS

    Catch him Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. MongoDB 的 MapReduce 大数据统计统计挖掘

    MongoDB虽然不像我们常用的mysql,sqlserver,oracle等关系型数据库有group by函数那样方便分组,但是MongoDB要实现分组也有3个办法: * Mongodb三种分组方式 ...

  9. ngui点击与场景点击判断

    注:NGUI 组件上加上 BoxCollider 并设置区域大小 public void OnMouseDown()    { if (UICamera.hoveredObject == null) ...

  10. Sqlserver统计语句

    --查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...