Java [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 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)
解题思路
对于这样的无序数组首先进行升序排列,使之变成非递减数组,调用java自带的Arrays.sort()方法即可。
然后每次固定最小的那个数,在后面的数组中找出另外两个数之和为该数的相反数即可。
具体的过程可参考博客:http://blog.csdn.net/zhouworld16/article/details/16917071
代码实现:
public class Solution {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> threeSum(int[] nums) {
int length = nums.length;
if (nums == null || length < 3)
return ans;
Arrays.sort(nums);
for (int i = 0; i < length - 2; ++i) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
findTwoSum(nums, i + 1, length - 1, nums[i]);
}
return ans;
}
public void findTwoSum(int[] num, int begin, int end, int target) {
while (begin < end) {
if (num[begin] + num[end] + target == 0) {
List<Integer> list = new ArrayList<Integer>();
list.add(target);
list.add(num[begin]);
list.add(num[end]);
ans.add(list);
while (begin < end && num[begin + 1] == num[begin])
begin++;
begin++;
while (begin < end && num[end - 1] == num[end])
end--;
end--;
} else if (num[begin] + num[end] + target > 0)
end--;
else
begin++;
}
}
}
Java [leetcode 15] 3Sum的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [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 S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
随机推荐
- DEVICE_ATTR的使用
DEVICE_ATTR的使用 使用DEVICE_ATTR,可以在sys fs中添加“文件”,通过修改该文件内容,可以实现在运行过程中动态控制device的目的. 类似的还有DRIVER_ATTR,BU ...
- sysfs接口函数到建立_DEVICE_ATTR
sysfs接口函数到建立_DEVICE_ATTR 最近在弄Sensor驱动,看过一个某厂家的成品驱动,里面实现的全都是sysfs接口,hal层利用sysfs生成的接口,对Sensor进行操作. 说道s ...
- windows下编译php5.2.17这是闹哪样?
参考:http://demon.tw/software/compile-php-on-windows.html
- EXTJS 4.2 资料 控件之radiogroup 的用法
最近在EXTJS4.2开发项目,radiogroup的用法,主要是和grid之间的编辑功能:看了好多资料都不对,特此在这里备注记录 代码如下, 1.这是一段Win窗体上的两个单选按钮,设置单选按钮都是 ...
- 前端跨域之html5 XMLHttpRequest Level2
前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...
- 如何正确看待Linq的DistinctBy扩展和ForEach扩展
在微软标准的Linq中,并没有DistinctBy扩展和ForEach扩展,但在平时使用工作中却又经常需要使用到这两个功能,照理来说,微软在Linq中应该包含这两个扩展才对,可事实上为什么并没有呢?本 ...
- (转)[C++语法] 关键字typedef用法
转自http://www.cnblogs.com/SweetDream/archive/2006/05/10/395921.html C/C++语言中的typedef相信大家已经不陌生,本文对C/C+ ...
- IntelliJ IDEA 部署Tomcat及创建一个web工程
一.部署Tomcat 二.新建一个web工程 1.新建一个Project 2.现在建立一个简单的web工程,所以只勾选下面选中的,此外,本版本(IntelliJ IDEA 14.1.5只支持3.1版本 ...
- 解决eclipse“copy项目重命名后重新发布,项目名在地址栏仍然是原来的项目名”的问题
任务描述:复制项目spring_user并重命名为spring_user_test 一.通过按F2和以下方式可以修改目标项目在workspace的名字 点击项目右键选择properties,输入关键字 ...
- textarea中限制输入字符长度(实用版)
textarea称文本域,又称文本区,即有滚动条的多行文本输入控件,在网页的提交表单中经常用到.与单行文本框text控件不同,它不能通过maxlength属性来限制字数,为此必须寻求其他方法来加以限制 ...