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 ...
随机推荐
- WPF简单的口算案例
前几天在博客园,看到有博友利用Winform做了一个口算案例,于是我想把它移植在WPF程序中.Winform程序:http://www.cnblogs.com/ImYZF/p/3345452.html ...
- SecureCRT上传bash: rz: command not found(转载)
转载自:http://blog.csdn.net/zhangdaiscott/article/details/18141017 -bash: rz: command not found rz命令没找到 ...
- iOS本地动态验证码生成-b
用于ios本地动态生成验证码,效果如下: demo.gif 导入CoreGraphics.framework用于绘制图形 封装UIView,便捷使用,代码如下: AuthcodeView.h #imp ...
- hdu 3714 Error Curves(三分)
http://acm.hdu.edu.cn/showproblem.php?pid=3714 [题意]: 题目意思看了很久很久,简单地说就是给你n个二次函数,定义域为[0,1000], 求x在定义域中 ...
- 浅谈JavaSccript函数与对象
函数 解剖函数 function One(leve1 , leve2){ //code return leve1+leve2 } 注释: 形参不需要加上类型: return语句为可选,没有return ...
- PHP发送微信模版消息
public function payResult($params) { global $_GPC, $_W; $weid = $this->_weid; $order = pdo_fetch( ...
- 1048: [HAOI2007]分割矩阵 - BZOJ
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个),这样分割了(n-1)次后,原矩阵被分割成了 ...
- 839. Optimal Marks - SPOJ
You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...
- TDD 实践过程
聚合根是唯一能够允许充当进入聚合的逻辑途径的实体. 是唯一能够允许聚合外的对象持有的引用成员. 聚合根--决定数据库的入口,以为:
- uva 348
dp还是比较容易 关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...