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.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
代码如下:算法复杂度O(n^2)
class Solution {
public List<List<Integer>> threeSum(int[] num) {
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
if (num == null || num.length < 3)
return result;
Arrays.sort(num);
int lastNum = num[0] - 1;
for (int i = 0; i < num.length - 2 && num[i] <= 0; i++) {
if (num[i] != lastNum) {
result.addAll(find(num, i));
lastNum = num[i];
}
}
return result;
}
private ArrayList<List<Integer>> find(int[] array, int index) {
int i = index + 1;
int j = array.length - 1;
int lastI = array[index] - 1;
int lastJ = array[index] - 1;
ArrayList<List<Integer>> Lists = new ArrayList<List<Integer>>();
while (i < j) {
if (array[i] + array[j] + array[index] == 0) {
if (array[i] == lastI) {
i++;
} else if (array[j] == lastJ) {
j--;
} else {
lastI = array[i];
lastJ = array[j];
ArrayList<Integer> curList = new ArrayList<Integer>();
curList.add(array[i]);
curList.add(array[j]);
curList.add(array[index]);
Lists.add(curList);
i++;
j--;
}
} else if (array[i] + array[j] + array[index] > 0) {
j--;
} else {
i++;
}
}
return Lists;
}
}
注意几点:
1,如果遇到重复的数字,那么可以直接跳过。外层循环和内层循环都可以,因为都是排过序的,如果遇到同样的数字必然产生同样的结果。
2,具体实现的时候直接让lastNum等于开始的数字减一,保证第一个数字不会被跳过。
算法的核心思路是,首先排序。这个算法复杂度是O(n*logn)。接下来先依次找每一个数字,每找到一个数字,剩下的目标就是在剩下的数字中找到2个数,使他们之和为0。正因为排过序,所以找两个数字的过程算法复杂度可以下降到O(n)。也就是从两头往中间。每次如果和大于0,就说明在外层数字确定的情况下,这两个数字的和大了,也就要让其变小,让j--,反之则i++。
3sum问题的解决的更多相关文章
- 《LeetBook》leetcode题解(18) : 4Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 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 ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- 16 3Sum Closest(输出距离target最近的三个数的和Medium)
题目意思:给一个数组,给一个target,找三个数的和,这个和要与target距离最近,输出这个和 思路:这个题比3sum要稍微简单一点,如果需要优化,也可以去重,不过因为结果唯一,我没有去重. mi ...
- 3Sum探讨(Java)
探讨一下leetcode上的3Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
随机推荐
- Mac 热键大全
屏幕捕捉快捷键动作............................保存到............-快捷键 全屏捕捉........................桌面(.PDF文件)..... ...
- 通过JAXB完成Java对象与XML之间的转换
Java对象转换XML的过程叫marshal. XML转换到Java对象的过程叫unmarshal. 一.Java对象转化为XML 这里省略getter和setter方法 通过标注@XMLRootEl ...
- 12款简化 Web 开发的 JavaScript 开发框架
前端框架简化了开发过程中,像 Bootstrap 和 Foundation 就是前端框架的佼佼者.在这篇文章了,我们编制了一组新鲜的,实用的,可以帮助您建立高质量的 Web 应用程序的 JavaScr ...
- 由于目标计算机积极拒绝,无法连接。 192.168.1.106:8078 说明: 执行当前 Web 请求期间,出现未经处理的异常。
请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.Net.Sockets.SocketException: 由于目标计算机积极拒绝,无法连接. 1 ...
- 在Visual Studio 2015 中添加SharePoint 2016 开发模板
前言 SharePoint 2016已经发布很久了,然而,默认安装VS2015以后,却没有SharePoint 2016的开发模板.其实问题很简单,和VS2012开发SharePoint 2013一样 ...
- VBA常用代码解析
031 删除工作表中的空行 如果需要删除工作表中所有的空行,可以使用下面的代码. Sub DelBlankRow() DimrRow As Long DimLRow As Long Dimi As L ...
- gitflow以及git
git大概只会用常用的那几个命令,自己用的最多的也只是add commit push pull之类的,然后那天电话面试问我版本回退怎么办我都忘记了. 然后又看了看教程,此时想起来做项目的时候师兄说的g ...
- T-SQL 如何获取一个表的列名
方法1: exec sp_columns [{table_name}],[{schema_name}] 方法2: SELECT * FROM syscolumns WHERE id=OBJECT_ID ...
- Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b error: Illegal ...
- error at ::0 can't find referenced pointcut messageInsertAspect
解决方法: http://stackoverflow.com/questions/10482563/aop-java-lang-illegalargumentexception-error-at-0- ...