【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 ...
随机推荐
- Hadoop2.6.0 完全分布式搭建
1 搭建这个完全分布式的前提环境: 配置好了jdk环境 配置好了ssh免密码登录(详见我的另外一篇博客,专门说ssh免密码登录) 我这里设置了四台机器,一台做master,另外三台做slave,hos ...
- vsCode怎么为一个前端项目配置ts的运行环境
vsCode为一个前端项目配置ts的运行环境,ts文件保存的时候自动编译成js文件: 假设此前端项目名称为Web:文件结构如图 1. 在根目录中新建一个“.vscode”文件夹,里面建一个“tasks ...
- thead tfoot tbody标签的使用
这三个都是<body>元素的子标签,不常用,因为其只是对<tr>标签做了一个区分 <thread>用于包裹表格头信息 <tfoot>用于包裹表格最后一行 ...
- Elasticsearch1.x 和Elasticsearch2.x 拼音分词插件lc-pinyin安装教程
Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文.拼音.同义词搜索 https://blog.csdn.net/chennanymy/article/category/60 ...
- kafka卡顿
一次kafka卡顿事故排查过程 https://www.cnblogs.com/yougewe/p/8975550.html 由于一次功能上线后,导致某数据量急剧下滑,给我们紧张的呢!排查过程也是个学 ...
- thrift的lua
thrift的lua实现 最近要进行系统升级,后台的数据是根据城市区分的.担心新系统的稳定性及新数据的准确性,计划部分城市采用新接口.接口的入参里没有城市信息,只有经纬度坐标,需要调用一个thrift ...
- [CF1019A]Elections
题目大意:有$n$个人,$m$个政党,每个人都想投一个政党,但可以用一定的钱让他选你想让他选的政党. 现在要$1$号政党获胜,获胜的条件是:票数严格大于其他所有政党.求最小代价 题解:暴力枚举其他政党 ...
- [洛谷P2657][SCOI2009]windy数
题目大意:不含前导零且相邻两个数字之差至少为$2$的正整数被称为$windy$数.问$[A, B]$内有多少个$windy$数? 题解:$f_{i, j}$表示数有$i$位,最高位为$j$(可能为$0 ...
- 周记【距gdoi:126天】
这周比上周好了那么一点点……但还是有点呵呵 搞了仙人掌图(当然不是动态的……),以及一个远古算法2-sat(神奇的对称性运用,需要巨大脑洞的建边). 然后关于高考不加分竞赛被“打压”……大神们都发表了 ...
- 导致SQL执行慢的原因
索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...