题目描述:

  给出一个有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-三数之和的更多相关文章

  1. lintcode: 三数之和II

    题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = .  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...

  2. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  3. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. LeeCode数组第15题三数之和

    题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  8. LeetCode第十六题-找出数组中三数之和最接近目标值的答案

    3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...

  9. 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和

    问题 A: 变位词 时间限制: 2 Sec  内存限制: 10 MB提交: 322  解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...

  10. python三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

随机推荐

  1. Windows下安装storm-0.9.1

    Windows下安装storm-0.9.1的详细步骤如下: 1.确定已经正确安装JDK1.6或JDK1.7(具体安装步骤略) 2.安装Python2.7版本(测试storm-starter proje ...

  2. 指针和Const限定符

    指针和Const限定符 1.指向const对象的指针 如果指针指向的是const对象,则不允许使用指针来改变其所指的const值.C++要求指向const对象的指针具有const特性. const d ...

  3. oracle tns

    oracle tns 是oracle提供的服务名,设置方法,oracle安装根目录---product----版本选择11.2.0----client1---NETWORK---ADMIN---tns ...

  4. gridview合并相同的行

    #region 方法:合并Gridview行    /// <summary>    /// 合并GridView指定行单元格    /// </summary>    /// ...

  5. 从一句SQL得出的启示

    select count(*) + 1 from `table` where rank > (select rank from `table` where id = *) 上面那句SQL 给了我 ...

  6. Asp.net 获取服务器指定文件夹目录文件,并提供下载

    string dirPath = HttpContext.Current.Server.MapPath("uploads/"); if (Directory.Exists(dirP ...

  7. android入门——UI(1)

    一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...

  8. Deep Learning(深度学习)相关网站

    Deep Learning(深度学习) ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习):一 ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习): ...

  9. NSURLConnect 的简单实用(iOS8淘汰)

    Demo_1 NSRULConnection NSRULConnection 苹果公司在ios8已经抛弃了,但是我还是要讲一下,因为这和后面的NSSession有着密切的联系 下面开始使用步骤: 1. ...

  10. js浮点数精度问题

    大多数语言在处理浮点数的时候都会遇到精度问题,但是在JS里似乎特别严重,来看一个例子 alert(45.6*13); 结果居然是592.800000000001,当然加法之类的也会有这个问题 那这是j ...