3Sum Closest——LeetCode
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).
题意就是给定一个数组,一个target,在数组中寻找一个三元组之和最接近target的值,这个题是之前3Sum的变形,不同的是这个题没有重复元素,而且设定解唯一。
我的思路是:跟之前的3sum一样,保留左右各一个pointer,遍历的时候,有left和right两个游标,left就从i+1开始,right就从数组最右length-1开始,如果i、left、right三个元素加起来等target,那么就可以加入结果的List中了,如果sum-target>0,那么说明sum比较大,right应该向左移动,反之left向右移动。
Talk is cheap>>
public int threeSumClosest(int[] num, int target) {
if (num == null || num.length < 3) {
return 0;
}
int min = Integer.MAX_VALUE;
int res=0;
Arrays.sort(num);
for (int i = 0; i < num.length - 2; i++) {
int left = i + 1;
int right = num.length - 1;
while (left < right) {
int sum = num[i] + num[left] + num[right];
// System.out.printf(i+" "+sum);
if (sum == target) {
return target;
}
if (min>=abs(sum-target)){
min = abs(sum-target);
res=sum;
}
if (sum-target>0){
right--;
}else {
left++;
}
}
}
return res;
}
public int abs(int a){
return Math.abs(a);
}
3Sum Closest——LeetCode的更多相关文章
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
- 3Sum Closest leetcode java
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 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 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- 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 ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- Linux Kernel: buffers和cached的区别
The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to op ...
- Gradle Import Wizard--官方文档
Last updated and checked to work with version 3.0.0 of the tools This tutorial will take you through ...
- Android Studio 快捷键(转)
Android Studio 快捷键 操作 Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ctrl + ...
- 关于C#中get和set
在看书的时候看见了一段代码,有两个类person: public class person { public string name; } public class person { public s ...
- 免写前缀JS包--prefixfree.min.js--插件
/** * StyleFix 1.0.3 & PrefixFree 1.0.7 * @author Lea Verou * MIT license */ (function(){functio ...
- (转)PHP中extract()函数的妙用
近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...
- SystemConfig.getPropertyValue("test");配置文件已经加了test=abc,但是取得时候空字符串
1.定位tomcat中System.properties是否配置了,发现配置了 2.定位myeclipse中修改的tomcat是不是自己配置的tomcat.发现是 3.定位如下位置配置是否读取我先在用 ...
- 关于线程池ThreadPool的学习
学习重点ThreadPool.SetMinThreads(out workerThreads, out completionPortThreads).这是整个线程池的关键. 而ThreadPool. ...
- 你好,C++(9)坐216路公交车去买3.5元一斤的西红柿——C++中如何表达各种数值数据 3.3 数值数据类型
3.3 数值数据类型 从每天早上睁开眼睛的那一刻开始,我们几乎每时每刻都在与数字打交道:从闹钟上的6点30分,到上班坐的216路公共汽车:从新闻中说的房价跌到了100元每平米到回家买菜时的西红柿3. ...
- 【USACO 3.2.3】纺车的轮子
[描述] 一架纺车有五个纺轮,这五个不透明的轮子边缘上都有一些缺口.这些缺口必须被迅速而准确地排列好.每个轮子都有一个起始标记(在0度),这样所有的轮子都可以在统一的已知位置开始转动.轮子按照角度变大 ...