【LeedCode】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: 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] ]
分析:
暴力法,三层for循环,时间复杂度为O(n^3),,这显然不是一种好的解决方式
我的做法是先对数组进行排序,这样负数在排在前面(负数越小,则它的绝对值越大),0在中间(如果存在0),正数在后面。排序算法的时间复杂度是O(nlgn)
然后从左往右取负数,从右往左取正数。即从两边向中间扫描。两个数求和的情况则刚好可以两边各取一个数进行判断,对于三个数求和的情况,我们可以先确定一个数,然后再去两个数的和为第一个取出的数的相反数即可。
注:当数组长度小于3时,直接返回null
public List<List<Integer>> func(int[] num) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int len = num.length;
if(num==null || len<3){
return null;
}
Arrays.sort(num);
for(int i =0;i<len-2;i++){
if(i>0 && num[i] == num[i-1]){
continue;
}
int j = i+1;
int k = len-1;
int target = -num[i];
while(j<k){
if(num[j]+num[k]==target){
result.add(Arrays.asList(num[i],num[j],num[k]));
j++;
k--;
while(j<k && num[j]==num[j-1]){
j++;
}
while(j>k && num[k]==num[k+1]){
k--;
}
}else if(num[j]+num[k]>target){
k--;
}else{
j++;
}
}
}
return result;
}
【LeedCode】3Sum的更多相关文章
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【Leetcode】【Medium】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 ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【leedcode】longest-substring-without-repeating-characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leedcode】add-two-numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- lintcode-49-字符大小写排序
49-字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 注意事项 小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置. 样例 给出"abAc ...
- web相关基础知识1
2017-12-13 09:47:11 关于HTML 1.绝对路径和相对路径 相对路径:相对于文件自身为参考. (工作中一般是使用相对路径) 这里我们用html文件为参考.如果说html和图片平级,那 ...
- Redis数据类型及操作详解
Redis数据库,是nosql的一种.与传统关系型数据库(如mysql.sqlserver等)相比,他在处理大数据量上相当有优势,扩展性和可用性高,这是传统型数据库所达不到的. Redis是一个key ...
- Win7/8, convert dynamic disk volume to basic volume.
之前不小心用了Win8自带的Disk Management 来调整磁盘分区的大小,当时跳出来一个warning窗口,说如果继续操作会变成dynamic disk,然后xxxx. 我心想都是Window ...
- 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组
题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...
- 【bzoj2661】[BeiJing wc2012]连连看 最大费用最大流
题目描述 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y ...
- [LG4890]Never·island DP
---题面--- 题解: 感到此题非常的神奇...看了大佬的题解才懂的. 首先建模: 先把所有队伍的出去回来时间都放在一个数组里,然后排序,从左到右扫一边,给每个队伍都建一个带权点,进行如下操作: ( ...
- BZOJ 1023: [SHOI2008]cactus仙人掌图 | 在仙人掌上跑DP
题目: 求仙人掌直径 http://www.lydsy.com/JudgeOnline/problem.php?id=1023 题解: 首先给出仙人掌的定义:满足所有的边至多在一个环上的无向联通图 我 ...
- URAL1696 Salary for Robots
题目戳这里. 最长下降子序列单调队列求法. \(f_{i,j,k}\)表示考虑前\(i\)个数,\(g_1 = j,g_2 = k\)的方案数.转移: \[f_{i,j,k} = \sum_{p = ...
- C# 序列化原因 (转)
1.什么是序列化 序列化是将对象状态转换为可保持或传输的格式的过程,在序列化过程中,对象的公共字段和私有字段以及类的名称(包括包含该类的程序集)都被转换为字节流,然后写入数据流.与序列化相对 ...