【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 ...
随机推荐
- atitit.java方法属性赋值and BeanUtils 1.6.1 .copyProperty的bug
atitit.java分配给属性值方法and BeanUtils 1.6.1 .copyProperty的bug 1. core.setProperty(o, "materialId&quo ...
- SQLserver2012 tcp/ip 1433port问题解决方法
非常多MSSQL安装完毕后,调用1433(默认port)是失败的,这边具体介绍下解决方法. 一..我们须要在电脑上开启telnet服务,定位问题须要.在cmd下使用telnet,假设报命令不存在说明没 ...
- Socket开发
Socket开发框架之消息的回调处理 伍华聪 2016-03-31 20:16 阅读:152 评论:0 Socket开发框架之数据加密及完整性检查 伍华聪 2016-03-29 22:39 阅 ...
- WPF对于xml的简单操作(下下)插入节点并排序
正如T所说,下下,这个方法不堪入目, ̄□ ̄|| 贴上再说 //先搞个struct声明 private struct datastruct { public string x; public strin ...
- hdu 4882 ZCC Loves Codefires(数学题+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ------------------------------------------------ ...
- Mahout推荐算法ItemBased
Mahout推荐的ItemBased 一. 算法原理 (一) 基本的 下面的例子,参见图评分矩阵:表现user,归类为item. 图(1) 该算法的原理: 1. 计算Item之间的相似度. ...
- android-adb通用
- Java RMI(远程方法调用) 实例与分析
目的: 通过本文,可以加深对Java RMI的理解,知道它的工作原理,怎么使用等. 也为了加深我自己的理解,故整理成文.不足之处,还望指出. 概念解释: RMI(RemoteMethodInvocat ...
- 特征选择(三)-K-L变换
上一讲说到,各个特征(各个分量)对分类来说,其重要性当然是不同的. 舍去不重要的分量,这就是降维. 聚类变换觉得:重要的分量就是能让变换后类内距离小的分量. 类内距离小,意味着抱团抱得紧. 可是,抱团 ...
- WebStorm主题设置
对于使用WebStorm作为开发工具的筒子们.应该忍受不了默认的主题吧,可是自己去一个一个设置又太繁琐.So,去网上下个主题那是必须的. 搜来一圈,发现一个站点提供了不少主题.闲话少说,进入正题. 1 ...