【LeetCode】15. 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 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]
]
思路:先对数组排序,for循环选定每个位置的数,在下个位置及末尾设置两个索引,从两边往中间走,找出两个数满足三者的和为0。因为数组有序,所以当和大于0时,右边索引左移,反之,左边索引右移。
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
int len=nums.length;
if(len<3) return result;
int sum=0;
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
if(nums[i]>0) break;
if(i>0&&nums[i]==nums[i-1]) continue;
int begin=i+1,end=len-1;
while(begin<end){
sum=nums[i]+nums[begin]+nums[end];
if(sum==0){
List<Integer> tem=new ArrayList<Integer>();
tem.add(nums[i]);
tem.add(nums[begin]);
tem.add(nums[end]);
result.add(tem);
begin++;end--;
while(begin<end&&nums[begin]==nums[begin-1]) begin++;
while(begin<end&&nums[end]==nums[end+1]) end--;
}else if(sum<0){
begin++;
}else end--;
}
}
return result;
}
}
【LeetCode】15. 3Sum 三个数和为0的更多相关文章
- [LeetCode] 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 ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- [LeetCode] 15. 3Sum ☆☆☆(3数和为0)
描述 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Fi ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- Leetcode 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 ...
- leetcode 15. 3Sum 双指针
题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. class Solution { public: vector<vec ...
- Leetcode 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 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
随机推荐
- php反射机制获取未知类的详细信息
使用ReflectionClass就可以获取未知类的详细信息 demo: require("hello.php"); $class = new ReflectionClass(&q ...
- windows类书的学习心得(转载)
原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...
- 更改AD查询LDAP条目的1000限制
解除LDAP导入时的AD条目查询限制 解除LDAP导入或读取AD用户数限制问题 更改AD查询LDAP条目的1000限制 来源:http://www.jiancool.com/article/55373 ...
- (二)java特征
java的核心是面向对象,与之相对的是面向过程的编程,在对整个java编程没有足够的理解和运用的情况下恐怕没办法很好的理解这两个概念. 在我的初步理解中,写一个程序就例如做一件事情,面向过程的 ...
- 【转】页面跳转Transfer与Redirect的区别你知道吗?
一 前言 关于页面跳转的方式常用的应该就是,链接跳转,js跳转,Server.Tranfser和Response.Redirect 这几种,可是在Tranfser与Redirect之间用哪种更好(本文 ...
- 使用 HTML5 Shiv 让 IE 支持 HTML5
HTML5 Shiv 使用 html5.js 必须在页面head元素内调用(因为 IE 必须在元素解析前知道这个元素,所以这个 JS 文件不能在页面底部调用.) 作者已经把js文件放在Google c ...
- java-Spring-1
1.@Autowired 自动寻找合适的类型注入,byType2.@Qualifier("userDAOImpl") 存在多个相同类型时,指定固定的一个bean,和上面1配合使用3 ...
- 6. redis持久化,认证
一. RDB 快照持久化 (默认) rdb机制,在满足redis.conf中配置的持久化策略后, 自动持久化数据, 默认存储在dump.rdb文件中 策略 : save 900 1 : 900秒内,一 ...
- CE_现金账户转账汇入汇出交易(案例)(未完成)
2014-07-15 BaoXinjian 一.摘要 二.案例 通过 Oracle Payments 结算事务处理 通过付款模板 事物处理子类型 已验证 -> 正在结算中 ->
- ajax提交后完全不进入action直接返回错误
今天遇到个问题就是jQuery提交ajax请求,居然没有进入action的断点而直接返回错误信息. 仔细排查后才发现原来是因为客户端提交的某字段是100w的值,而后台对应的字段是个Short类型,根本 ...