【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 ...
随机推荐
- libevent显式调用事件处理
) { SearchAcceptListen2(p_ev_arg->listen_fd,,¬ify_event,base); event_base_loop(base, EVLOO ...
- 【WebService】——入门实例
服务端 服务: 1.add(int a,int b) 2.minus(int a,int b) 具体如下: <pre name="code" class="java ...
- 基于网络的 Red Hat 无人值守安装
基于网络的 Red Hat 无人值守安装 本文介绍了 PC 平台上的一种快速 Red Hat Linux 安装方案.它具有很高的自动化程度--用户只需手工启动机器并选择从网络启动,就可以完成整个安装过 ...
- 【bzoj4976】宝石镶嵌 乱搞+dp
题目描述 从$n$个数中选出$n-k$个,使得它们的二进制或(or)最大.输出这个值. 输入 第一行包含两个正整数$n,k(2\le n\le 100000,1\le k\le 100,k<n) ...
- 使用HTML5的JavaScript选择器操作页面中的元素
<!doctype html><html lang="en"> <head> <meta charset="UTF-8& ...
- mysql的增量备份
1,在mysql 的my.ini文件中 2在我的D盘下创建binlog文件夹 3重启mysql服务可以看到我的D盘下的binlog下生成如下文件 4,在默认的test数据库中创建t_one1这张表 c ...
- 【COGS 2051】王者之剑 最小割
这个其实就是在说明相邻的点不能取,我们发现只要其满足这个条件他总能走出来,那么我们就最小割就是了,我们先黑白染色,S 一排黑点 一排白点 T 对于相邻的点我们就直接中间连INF,于是就满足只要一个点选 ...
- jQuery源码分析笔记
jquery-2.0.3.js版本源码分析 (function(){ (21,94) 定义了一些变量和函数 jQuery = function(){}; (96,283) 给JQ对象,添加一些方法 ...
- POJ1459:Power Network(多源点多汇点的最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 31086 Accepted: 15986 题 ...
- bzoj 1124 [POI2008]枪战Maf 贪心
[POI2008]枪战Maf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 741 Solved: 295[Submit][Status][Disc ...