【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人。您可能仍然想看看这个问题该怎么解决,然后看看他们的利益扩展它。
【称号】
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)
【解析】
K Sum问题是一个系列,博客 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 总结得比較完整,有兴趣能够去看。
作为刚開始学习的人。我们用最简洁的思路来说一下这道题怎么解。
暴力解决法是每一个人都能想到的。三层for循环,时间复杂度是O(n^3),并且还要处理反复的问题,显然不是题目想要的解法。
那能不能降到O(n^2)?排序算法的时间复杂度为O(nlgn),小于O(n^2),那么我们最好还是先对数组排个序。
排序之后,我们就能够对数组用两个指针分别从前后两端向中间扫描了,假设是 2Sum,我们找到两个指针之和为target就OK了。那 3Sum 类似。我们能够先固定一个数,然后找另外两个数之和为第一个数的相反数就能够了。
代码不难。先看了再说。
【Java代码】O(n^2)
public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>(); public List<List<Integer>> threeSum(int[] num) {
if (num == null || num.length < 3) return ret; Arrays.sort(num); int len = num.length;
for (int i = 0; i < len-2; i++) {
if (i > 0 && num[i] == num[i-1]) continue;
find(num, i+1, len-1, num[i]); //寻找两个数与num[i]的和为0
} return ret;
} public void find(int[] num, int begin, int end, int target) {
int l = begin, r = end;
while (l < r) {
if (num[l] + num[r] + target == 0) {
List<Integer> ans = new ArrayList<Integer>();
ans.add(target);
ans.add(num[l]);
ans.add(num[r]);
ret.add(ans); //放入结果集中
while (l < r && num[l] == num[l+1]) l++;
while (l < r && num[r] == num[r-1]) r--;
l++;
r--;
} else if (num[l] + num[r] + target < 0) {
l++;
} else {
r--;
}
}
}
}
注意,对于 num[i],寻找另外两个数时,仅仅要从 i+1 開始找就能够了。
这样的写法,能够避免结果集中有反复,由于数组时排好序的。所以当一个数被放到结果集随着时间的推移,它的背后,其相当于直接跳过。
版权声明:本文博主原创文章,博客,未经同意不得转载。
【LeetCode】3Sum 解决报告的更多相关文章
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- 【LeetCode】Triangle 解决报告
[称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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] 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: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 ...
随机推荐
- HDU 4790 Just Random 数学
链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...
- Android的PackageManager的使用
Android系统提供了很多服务管理的类,包括ActivityManager.PowerManager(电源管理).AudioManager(音频管理)以及PackageManager管理类.Pack ...
- SQLServer2012 分页语句执行分析
上一篇文章提到了,SQLServer2012在使用Offset,Fetch语句分页时,获取了大量不需要的数据,导致查询效率低的问题. 现在让我们来看看,究竟是什么导致SQLServer不能按需取数呢? ...
- VC中Tab control的用法
1. 新建一个MFC工程, 取名MyTab, 选择Dialog based, 然后Finish. 2. 删除对话框上默认添加的三个控件. 添加Tab Control控件并在Property属性中设置I ...
- 安装配置gerrit
Centos 安装配置gerrit 关闭selinux,不然nginx的反向代理会报错connect() to 127.0.0.1:8080 failed (13: Permission denied ...
- POJ 1182 :食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43526 Accepted: 12679 Description ...
- uva11922(强行用rope替代spaly)
spaly没学过,用rope水过, rope是extension库中的东西,codeblocks编译器支持, 需要包含 #include <ext/rope>using namespace ...
- Web Worker在WebKit中的实现机制
web worker 是执行在后台的 JavaScript,独立于其它脚本.不会影响页面的性能.这是HTML5的一个标准:实现上讲.浏览器为wokrer启动了新的线程,从而实现了异步操作的功能; 以下 ...
- bellman_ford寻找平均权值最小的回路
给定一个有向图,如果存在平均值最小的回路,输出平均值. 使用二分法求解,对于一个猜测值mid,判断是否存在平均值小于mid的回路 如果存在平均值小于mid的包含k条边的回路,那么有w1+w2+w3+. ...
- 【ALearning】第四章 Android Layout组件布局(一)
在本章中,我们将Android学习组件布局.在前面的章节,我们也开始使用LinearLayout布局.然后我们在布局文件更加具体的学习和理解,会. Android的界面是有布局和组件协同完毕的,布局好 ...