【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 ...
随机推荐
- 【iOS开发】多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
http://blog.csdn.net/crycheng/article/details/21799611 本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSI ...
- Servlet入门(2)
1.url_pattern匹配模式 2.servlet生命周期 3.servlet线程问题 一.url_pattern 1.定义: 当浏览器发起一个url请求后,该请求发送到servlet容器的时候, ...
- linux服务器su之后变成bash-4.1#
当前为root权限 cd /home/jboss 执行如下命令,将缺失的配置文件拷贝到指定位置即可 cp ./.bashrc /root cp ./.bash_profile /root 然后切换账号 ...
- [LeetCode] 65. Valid Number(多个标志位)
[思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...
- ashx文件和aspx
ashx文件和aspx文件有什么不同? 我们先新建一个ashx文件看看: <%@ WebHandler Language="C#" Class="Handler&q ...
- [CF409F]000001
题目大意:输入一个数,输出一个数(愚人节系列) 题解:$OEIS$的$A000001$(原来我不想写的,但是洛谷的智能推荐推荐我做这个...是不是我太菜了) 卡点:无 C++ Code: #inclu ...
- 2018牛客多校第三场 C.Shuffle Cards
题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...
- [bzoj] 1257 余数之和sum || 数论
原题 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的值,其中k mod i表示k除以i的余数. \(\sum^n_{i=1} ...
- BZOJ2299 [HAOI2011]向量 【裴蜀定理】
题目链接 BZOJ2299 题解 题意就是给我们四个方向的向量\((a,b),(b,a),(-a,b),(b,-a)\),求能否凑出\((x,y)\) 显然我们就可以得到一对四元方程组,用裴蜀定理判断 ...
- Palindrome [Manecher]
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12214 Accepted: 4583 Descript ...