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 ...
随机推荐
- Reso | liunx下longeneQQ和搜狗拼音
sogoupinyin_2.0.0.0078_amd64.deb: http://pan.baidu.com/s/1eSDLvEU WineQQ7.8-20151109-Longene .deb: ...
- C++初始化顺序
1. 全局和类的静态变量成员在main之前构造和初始化,静态成员不能在类的内部构造初始化 2. 类的普通成员依据在类内的定义顺序初始化,类的构造函数的初始化类表只能决定成员的构造函数,不能决定构造顺序 ...
- Linux防火墙配置
防火墙命令 service iptables stop --停止 service iptables start --启动文件 /etc/sysconfig/iptables # Firewall c ...
- State 状态模式
简介 状态模式允许一个对象在其内部[状态]改变的时候改变其[行为].这个对象看上去就像是改变了它的类一样. 用一句话来表述,状态模式把所研究的对象的[行为]包装在不同的[状态对象]里,[每一个状态对象 ...
- MVC4使用EF6连接mysql数据库
1.需要安装MySql.Data.Entity.EF6,此dll可以在项目——>管理NuGet程序包里联机搜索MySql.Data.Entity.EF6并安装即可 2.连接字符串需要添加prov ...
- 排名最重要的三个优化阶段分析 --------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )
网站排名,不是一两天能够决定的.要想取得好的排名,得分时间分阶段地做排名,网站优化分前期,中期,后期,怎么来区别不同的阶段该用怎样的优化手段.今晚SEO研究中心创始人Moon老师分享:排名最重要的三个 ...
- ThinkPHP学习 volist标签高级应用之多重嵌套循环、隔行变色(转)
Action代码: public function index(){ $prod = I("get.prod_en"); $id = I("get.id", 0 ...
- 滑动页面,顶部导航or顶部 固定在一个位置
现在很多页面 特别是电商用的比较多 比如电商里面某个商品的详细页 往下拉页面 当滚轮到达一定位置的时候 导航栏即固定在顶部 其实他的原理很简单, 就是一开始设置导航为相对定位,然后计算出滚动条离顶部 ...
- javascript判断值是否undefined
function isUndefined(variable) { return typeof variable == 'undefined' ? true : false; }
- 关于android MTK相机L版本,切换屏幕比例后,分辨率随之改变,但重新进入相机后原有分辨率不再生效问题
BUG详细:比如4:3的时候是200W,切成全屏变400W,重新切回4:3为300W,退出相机后,重新进入又变成200W. 原因分析:这个版本的设计如此,当你点选屏幕比例的时候,程序设计是把这个比例值 ...