LeetCode: 3SumClosest
Title :
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).
int threeSumClosest(vector<int> &num, int target){
sort(num.begin(),num.end());
int min = INT_MAX;
int result;
for (int i = ; i < num.size()-; i++){
if (i > && num[i] == num[i-])
continue;
int left = i+;
int right = num.size()-;
int goal = target - num[i];
while (left < right){
int diff = abs(target - num[left] - num[right] - num[i]);
if (diff < min){
min = diff;
result = num[left] + num[right] + num[i];
}
if (num[left] + num[right] < goal){
while (left < right && (num[left] == num[left+]))
left++;
left++;
}else if (num[left] + num[right] > goal){
while (left < right && (num[right] == num[right-]))
right--;
right--;
}else{
return target;
}
}
}
return result;
}
LeetCode: 3SumClosest的更多相关文章
- [Leetcode] 3sum-closest 给定值,最为相近的3数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 3sum-closest 题解
思路 排序 枚举一个数a 双指针移动法确定b和c 求和,更新最接近的值 复杂度 T(n)=O(n2)  M(n)=O(1)T(n)=O(n^2) \; M(n)=O(1)T ...
- leetcode 日记 3sumclosest java
思路一为遍历: public int thirdSolution(int[] nums, int target) { int result = nums[0] + nums[1] + nums[2]; ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
随机推荐
- 【BZOJ】【1385】【Baltic2000】Division expression
欧几里得算法 普通的求个gcd即可……思路题 因为要求尽量是整数……所以 $\frac{x_1}{x_2*x_3*x_4*....*x_n}$是最大的结果了,因为$x_2$必须为分母,$x_1$必须为 ...
- 【BZOJ】【2208】【JSOI2010】连通数
题解: 1.Tarjan缩点以后对每个连通分量进行深搜,看能到哪些连通分量,能到达的所有连通分量的size之和记为sum.则第i个连通分量对答案的贡献为size[i]*sum(到其他连通分量)+siz ...
- sampler2d
Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{ Texture = <texture_ ...
- JS内存管理测试
打开调试器,切换到timer,点击左下角的record按钮开始,切换到memory视图,在文档中点击鼠标左右键,看股价走势图 function Allocate(kbs){ this.mem = ne ...
- getHibernateTemplate()为NUll
getHibernateTemplate()为NUll,困扰好几天了,网上也找了好些方法一直解决不掉15 小弟刚刚开始学SSH,是用的Struts2+Hibernate+Spring,运行的时候发现g ...
- 使用NodeJs,实现数据抓取
学习笔记 前言 近期做一个数据抓爬工具,最开始使用的是C#控制台应用,同时正则表达式去过滤数据,看着还行,可每次运行都依附于.net framework很是不爽,于是想整点其他的方法.本人还是比较喜欢 ...
- G家二面
题目都很基本,都属于听说过但是不会做的…都是操作系统,compiler的概念题… 概念题郁闷就郁闷在不会就是不会,就算能扯两句也会被问倒… 算法就一个,pow(x, y),5分钟不到…… 不是听说G家 ...
- POJ 1330 Nearest Common Ancestors(求最近的公共祖先)
题意:给出一棵树,再给出两个节点a.b,求离它们最近的公共祖先.方法一: 先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root. 之后 ...
- VisualSvn Server介绍
1 .VisualSvn Server VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和Visual Studio集成在一起,但是不免费 ...
- 【转载】Eclipse自动编译问题
今天调试的时候发现问题:调试的时候竟然在我注释的里面走,当时那个郁闷啊,每次都要clean下才可以,晚上感觉不对劲,上网查了查,原来是bulid automatically这个我把勾去掉了,下面是原文 ...