题目描述:

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

解题思路:

这个跟3Sum的思路差不多,不过实现的过程就简单多了。外面一个大的循环,里面也是从两边开始,如果三个数之和减去目标值的绝对值较小,则更新之并记录此三个数之和。

代码如下:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int length = nums.length;
int min = Integer.MAX_VALUE;
int left, right, comp, result = 0; if (length < 3)
return 0;
Arrays.sort(nums); for (int i = 0; i < length - 2; i++) {
left = i + 1;
right = length - 1;
while (left < right) {
comp = nums[i] + nums[left] + nums[right];
if (Math.abs(comp - target) < min){
min = Math.abs(comp - target);
result = comp;
}
if (comp > target)
right--;
else if (comp < target)
left++;
else
return target;
}
}
return result;
}
}

Java [leetcode 16] 3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. leetcode 16. 3Sum Closest JAVA

    题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...

  4. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  5. LeetCode 16. 3Sum Closest. (最接近的三数之和)

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

  6. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

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

  7. Leetcode 16. 3Sum Closest

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

  8. [LeetCode] 16. 3Sum Closest 解题思路

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

  9. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

随机推荐

  1. fedroa 更换字体

    1. 添加RPM包: rpm -Uvh http://www.infinality.net/fedora/linux/infinality-repo-1.0-1.noarch.rpm 2. 安装包 y ...

  2. SVN备份教程(一)

    最近一段时间在项目中用到了SVN备份的相关内容,这里给大家做一个简单的教程,重点在于SVN备份环境的搭建过程中,大家学到的解决问题的思维方式. 1.分类 SVN备份主要分为两种:一种是远程备份,另一种 ...

  3. Qt智能指针简明说明

    下面的智能指针分别对应boost库,Qt库,c++11的智能指针 boost::scoped_ptr  QScopedPointer unique_ptr 在其生命期结束后会自动删除它所指的对象(确定 ...

  4. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. springside springmvc 的一个SB问题

    <form  id="inputForm" modelAttribute="order" action="${ctx}/myorder/orde ...

  6. [转载]MongoDB查询优化原则

    .在查询条件.排序条件.统计条件的字段上选择创建索引,可以显著提高查询效率. .用$or时把匹配最 多 结果的条件放在最前面,用$and时把匹配最 少 结果的条件放在最前面. .使用limit()限定 ...

  7. Hex string convert to integer with stringstream

    #include <sstream>#include <iostream>int main() { unsigned int x; std::stringstream ss; ...

  8. hdu 1851 A Simple Game 博弈论

    简单博弈问题(巴什博弈-Bash Game) 巴什博弈:只有一堆n个物品,两个人轮流从这对物品中取物,规定每次至少取一个,最多取m个,最后取光着得胜. 很容易想到当n%(m+1)!=0时,先取者必胜, ...

  9. codeforces #310 div1 B

    我们考虑n-1座桥每座桥需要的长度在一个区间[L,R]中 之后我们现在有m座桥,每个桥的长度为k 题意就是要求一个匹配方案 显然如果数据范围不大直接KM就可以了 可是20w的数据KM显然要T 所以我们 ...

  10. Qt: 读写二进制文件(写对象, 原始数据等)

    #include <iostream>#include <QFile>#include <QImage>#include <QMap>#include ...