【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 ...
随机推荐
- 【足迹C++primer】40、动态数组
动态数组 C++语言定义了第二种new表达式语法.能够分配并初始化一个对象数组.标准库中包括 一个名为allocator的类.同意我们将分配和初始化分离. 12.2.1 new和数组 void fun ...
- 安装pygame
pygame的安装 我们首先要去到:http://www.pygame.org/download.shtml 下载我们所需要的软件包: 我选择的是:pygame-1.9.2a0.win32-py3.2 ...
- CF552E 字符串 表达式求值
http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...
- Matlab hermite
保形分段三次hermite插值 % 这是MATLAB里面的pchip.m文件.这里把它的凝视改写成汉语,主要是想弄清楚它是怎么计算在节点处的导数的. function v = pchip(x,y,xx ...
- 悟道—位IT高管20年的职场心经(读书笔记五)
悟道--一位IT高管20年的职场心经 第五章 搞定老板 "老板就是老板" 这一点,你可能会忘了,他一定不会忘: "老板不会总是老板" 这一点,他可能会忘,你最好 ...
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
4 三维 4.1 共享三维场景 用户能够将ArcScene文档导出为3D web场景,能够被加载到ArcGIS Online.Portal或本地Web服务器上并进行分享.这样,用户可以 ...
- 【译】手动处理Team Foundation Server 2010 数据仓库和分析服务数据库
原贴地址:Manually Processing the Team Foundation Server 2010 Data Warehouse and Analysis Services Databa ...
- 局域网连接SQL Server数据库配置
首先要保证两台机器位于同一局域网内,然后打开配置工具→SQL Server配置管理器进行配置.将MSSQLSERVER的协议的TCP/IP的(IP1.IP2)TCPport改为1433,已启用改为是. ...
- Chromium
Chromium多进程架构 多进程架构 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Bac ...
- VBoxGuestAdditions.iso下载
http://download.virtualbox.org/virtualbox/4.1.2/VBoxGuestAdditions_4.1.2.iso 其他版本可能反过来确定..