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 ...
随机推荐
- (转)Qt Model/View 学习笔记 (四)——创建新的Models
创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...
- Device eth0 does not seem to be present
解决办法: 首先,打开/etc/udev/rules.d/70-persistent-net.rules内容如下面例子所示: # vi /etc/udev/rules.d/70-persistent- ...
- 【发问】代表ODBC、Ibatis 发问 Hibernate、Linq、Entity、JPA
分页: 多表关联查询: 多表操作 临时表: 存储过程式长语句 : Hibernate与iBATIS的比较 博客分类: db iBATISHibernateSQL数据结构ORM 1.出身 hibern ...
- EXTJS 4.2 资料 控件之Window窗体自动填充页面
1.html页面代码: <div id="component" style="width:100%;height:100%"> <body&g ...
- 【BZOJ 1189】[HNOI2007]紧急疏散evacuate
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...
- drupal CMS
http://drupalchina.cn/ https://www.drupal.org
- C++中头文件相互包含与前置声明
一.类嵌套的疑问 C++头文件重复包含实在是一个令人头痛的问题,前一段时间在做一个简单的数据结构演示程序的时候,不只一次的遇到这种问题.假设我们有两个类A和B,分别定义在各自的有文件A.h和B.h中, ...
- this.Invoke和this.BeginInvoke的区别
private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...
- Does not contain a valid host:port authority: Master:8031 (configuration property 'yarn.resourcemanager.resource-tracker.address')
问题解决: 这个错误是:yarn里面的配置的格式有错误:如: <property> <name>yarn.resourcemanager.address</name> ...
- 4.3 spring-嵌入式beans标签的解析
对于嵌入式的beans标签,想信大家很少使用过,或者接触过,起码,我本人就没用过. 它非常类似于Import标签所提供的功能; 使用如下: <?xml version="1.0&quo ...