lintcode(57)三数之和
来源:互联网 发布:比比多味豆淘宝 编辑:程序博客网 时间:2024/06/05 02:42
描述:
给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
样例:
给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
注意事项
在三元组(a, b, c),要求a <= b <= c。
结果不能包含重复的三元组。
思路:
是用二分法确定后两个元素
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) { // write your code here ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(numbers == null || numbers.length<3){ return result; } int len = numbers.length; Arrays.sort(numbers); for(int i = 0;i<len-2;i++){ int start = i + 1; int end = len - 1; while(start < end){ ArrayList<Integer> temp = new ArrayList<Integer>(); if(numbers[start] + numbers[end] == -numbers[i]){ temp.add(numbers[i]); temp.add(numbers[start]); temp.add(numbers[end]); if(!result.contains(temp)){ result.add(temp); } start++; end--; }else if(numbers[start] + numbers[end] > -numbers[i]){ end--; }else{ start++; } } } return result; }}
0 0
- lintcode-三数之和-57
- LintCode 57-三数之和
- lintcode(57)三数之和
- LintCode 57 三数之和
- LintCode 三数之和
- LintCode 三数之和
- LintCode: 三数之和
- lintcode,三数之和
- Lintcode 三数之和
- [Lintcode]三数之和
- lintcode--三数之和
- lintcode 三数之和
- [LintCode 57] 三数之和(Python)
- LintCode(M)三数之和
- LintCode两数之和,三数之和,四数之和
- 【lintcode】两数之和、三数之和、最接近的三数之和、四数之和小结
- LintCode 三数之和(二)
- 三数之和——LintCode
- 跟随屏幕滚动后固定导航到顶端
- java小实现map家族
- java----------华为机试------------合并表记录
- Programming over R
- spring源码(8)注册解析的BeanDefinition
- lintcode(57)三数之和
- Machine Learning知识点一览
- NYOJ 303
- 多源有权图的最短路径 floyd算法(动态规划能解决负权边)7.1.3
- 微博视频怎么下载?微博视频下载和保存工具
- Java中List转换为数组,数组转List
- Linux文件权限
- APP安全之APK完整性校验
- Android 开发SD卡操作