【数组】3Sum
题目:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
思路:
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
var n=nums.length,temp=0,res=[];
if(nums.length<3){
return [];
}
nums.sort(function(a,b){return a-b;});
for(var i=0;i<n-2;i++){
if(nums[i]>0){
break;
}
for(var j=n-1;j>i+1;j--){
temp=-(nums[i]+nums[j]);
var l=i+1,r=j-1;
if(nums[l]>temp||nums[r]<temp){
break;
}
while(l<=r){
var mid=l+Math.floor((r-l)/2);
if(nums[mid]==temp){
var arr=[nums[i],nums[mid],nums[j]];
res[res.length]=arr;
break;
}else if(nums[mid]<temp){
l=mid+1;
}else{
r=mid-1;
}
}
}
} return res;
};
【数组】3Sum的更多相关文章
- 3sum(从数组中找出三个数的和为0)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 15. 3Sum (重新分配数组大小)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [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] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 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
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- No.016:3Sum Closest
问题: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- No.015:3Sum
问题: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?Find all ...
随机推荐
- Paper格式-国际会议版
Paper Title 论文题目 Authors Name/s per 1st Affiliation (Author) 作者名字/s 每第一作者 line 1 (of Affiliation): d ...
- hibernate从浅至深
hibernate在开发中的位置 web------>struts2--------->hibernate ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对 ...
- 几个CSS-content的小例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Hibernate和spring中的session总结
1.this.getSession() 是org.springframework.orm.hibernate3.support.HibernateDaoSupport 中的一个方法,它可以从当前事务或 ...
- WPF/Silverlight开发的15个最佳实践(转发)
英文出处:http://www.kunal-chowdhury.com/2010/08/some-best-practices-for-silverlight.html 作者:Kunal Chowdh ...
- oauth入门
oauth可以支持跨网站的数据传输.假设一个用户把照片上传到faji网站,然后想登录到beppa网站(照片打印),把faji的上照片打印出来. 她当然可以自己把照片取下来再上传上去,不过比较麻烦. 使 ...
- python 判断是否为中文
python在执行代码过程是不知道这个字符是什么意思的.是否是中文,而是把所有代码翻译成二进制也就是000111这种形式,机器可以看懂的语言. 也就是在计算机中所有的字符都是有数字来表示的.汉字也是有 ...
- asp.net接收传入的数据流
我们在日常的应用中,都会遇到这样一个问题,就是我们做的asp.NET程序,会收到其它第三方软件传过来的一些信息数据流,当然了一些文本形式的信息,可以采用get或post的方法来接收,可是要是传过来的是 ...
- 使用ABP框架踩过的坑系列1
企业级(例如ERP)应用, 一遍一遍的在重复:认证.验证.异常处理.日志.国际化和本地化.数据库连接管理.配置管理. 审计记录等,同时.NET有很多最佳实践:分层.模块化.DDD领域驱动.DI ...
- 如何连接MSSQLSERVER2014(其他版本数据库大体相同)
购买了服务器后我在上面安装了SqlServer2014,但是程序中总是远程连接不上,特此记录一下: 一.一些基本设置如下链接: 转载自:https://blog.sun0816.com/20381.h ...