LintCode-三数之和
题目描述:
给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
注意事项
在三元组(a, b, c),要求a <= b <= c。
结果不能包含重复的三元组。
如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是:
(-1, 0, 1)
(-1, -1, 2)
public class Solution {
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numbers == null || numbers.length<3)
return result;
Arrays.sort(numbers);
for(int i = 0;i<numbers.length; i++){
int left = i+ 1;
int right = numbers.length - 1;
while(left < right){
int sum = numbers[i] + numbers[left] + numbers[right];
ArrayList<Integer> path = new ArrayList<Integer>();
if(sum==0){
path.add(numbers[i]);
path.add(numbers[left]);
path.add(numbers[right]);
if(result.contains(path)==false)
result.add(path);
left++;
right--;
}else if(sum>0){
right--;
}else{
left++;
}
}
}
return result;
}
}
LintCode-三数之和的更多相关文章
- lintcode: 三数之和II
题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = . 和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- LeetCode第十六题-找出数组中三数之和最接近目标值的答案
3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...
- 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和
问题 A: 变位词 时间限制: 2 Sec 内存限制: 10 MB提交: 322 解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...
- python三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
随机推荐
- ceph 块设备
数据的存储设备? 数据的存储有3种形式,1种是直接以二进制数据的形式存储在裸设备(包括块设备)上,另外一种是以文件的形式经过文件系统管理进行存储.第三种就是以对象的形式进行对象存储.本篇讨论围绕着块设 ...
- asp.net 向后台提交 html 代码段 包括 <> 标签
首先 在默认情况向标签类的东西是不会让你提交的 这是出于.net 的默认安全机制 我们要先在 <%@ page %> 里边加上 ValidateRequest="false&q ...
- 转|in、exists、join效率
EXISTS.IN与JOIN,都可以用来实现形如“查询A表中在(或不在)B表中的记录”的查询逻辑. 在查询的两个表大小相当的情况下,3种查询方式的执行时间通常是:EXISTS <= IN < ...
- UICollectionView 简单的使用和注意事项
UICollectionView 在创建的时候,要给它一个UICollectionViewFlowLayout (不然会崩溃),就像tableview一样,也要为它注册自定义的cell. UIColl ...
- android—资源文件(res)的引用
android项目中res目录下的文件含义各不相同主要有两种方式引用它们 例子:在string.xml中定义一个<string name="hello">Hello W ...
- nginx install
./configure --prefix=/home/allen.mh/local/nginx --with-http_ssl_module --with-http_sub_module --with ...
- Nanjing GDG Meetup 10月19日线下活动
Nanjing GDG 10 月份的线下活动将在本周六 (10/19) 举办,这次会请到对所有开发者都有重要帮助的神器 GoAgent 的作者 Phus Lu 来给我们做一场分享,热烈欢迎大家报名参 ...
- android netty5.0 编译时 java.lang.NoClassDefFoundError: io.netty.channel.nio.NioEventLoopGroup
android netty5.0 编译时 java.lang.NoClassDefFoundError: io.netty.channel.nio.NioEventLoopGroup 复制netty包 ...
- javascript块级作用域
在c/java中,拥有块级作用域的概念,大括号内就是一个块级作用域,在块级作用域内声明的变量,块以外不可见. C语音的块级作用域示例如下: ,two = ; if(one < two){ ; t ...
- NET CORE 应用程序启动
原文:Application Startup作者:Steve Smith翻译:刘怡(AlexLEWIS)校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程序提供 ...