【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 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)
题解:把3Sum转换成2Sum,然后解决2Sum问题。
对于num中任意一个数num[i],我们要在i+1~num.length-1之间寻找两个数使得它们的和为0-num[i]。
而寻找两个数使得它们的和为0-num[i],我们可以设定两个指针start和last,我们知道在start+1~last之间我们要寻找的数是0-num[i]-num[start],这样三个数的和就是0了。所以如果
- last所指向的数比0-num[i]-num[start]大,我们把last指针往前移动;
- 如果比0-num[i]-num[start]小,我们把start指针往后移动(last指针不动是因为我们已经搜索过了可能和last指针后面组成答案的数,这些数在start之前);
- 如果相等,就把num[i],num[start]和num[last]三个数依次放在list中作为一组答案。然后越过start后面和start相等的数,继续循环。
图示如下:

例如题目的数组排序后得到{-4,-1,0,1,2},当i指向-1的时候,我们需要在{0,1,2}之中找到两个数和为0-(-1)=1,把start指向0,last指向2,那么我们接下来要利用last找到1,last目前指向2,比需要的数1大,所以前移last,wala,我们找到了需要的1,同时找到了一组答案[-1,0,1]。
去重的工作十分简单,除了上述说的越过start后面和start相等的数外,在最外层的for循环的时候,如果num[i] = num[i-1],那么也可以越过i。
最后代码如下:
public class Solution {
public List<List<Integer>> threeSum(int[] num) {
List<List<Integer>> answer = new ArrayList<List<Integer>>();
if(num == null || num.length == 0)
return answer;
Arrays.sort(num);
for(int i = 0;i < num.length;i++){
//remove duplicates
if(i != 0 && num[i] == num[i-1])
continue;
int twoSum = 0 - num[i];
int start = i + 1;
int last = num.length-1;
while(start < last){
int OneSum = twoSum - num[start];
//Found one solution
if(num[last] == OneSum){
ArrayList<Integer> result = new ArrayList<Integer>();
result.add(num[i]);
result.add(num[start]);
result.add(num[last]);
answer.add(result);
start++;
last--;
//remove duplicates
while(start < last && num[start] == num[start-1])
start++;
}
else if(num[last] > OneSum)
{
last--;
}
else{
start++;
}
}
}
return answer;
}
}
【leetcode刷题笔记】3Sum的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【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刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- LeetCode刷题笔记-回溯法-分割回文串
题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...
随机推荐
- CentOS6安装和卸载docker
系统版本 [root@bogon yum.repos.d]# uname -a Linux bogon 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 ...
- android监听事件的方式
1.匿名内部类 bt.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View view){//Here ...
- Selenium3.X 与 Javascript (Nodejs)
传送门 # 官网网站 http://docs.seleniumhq.org/download/ # API DOC http://goo.gl/hohAut # 慕课网教程http://www.imo ...
- CentOs上搭建nginx
目录 CentOs上搭建nginx 1. 在root环境下安装nginx 1.1 常用工具安装 1.2 关闭iptables规则 1.3 关闭SELinux 1.4 安装C/C++环境和PCRE库 1 ...
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- [译]GLUT教程 - 创建和关闭子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Creating and Destroying Subwind ...
- 对LCD接口的认识
LCD接口类型: 1.首先我们以传递的信号类型来区分主要有两大类:- 模拟信号: - VGA: Video Graphics Array- 数字信号 - TTL: Transistor Transis ...
- Educational Codeforces Round 27 F. Guards In The Storehouse
F. Guards In The Storehouse time limit per test 1.5 seconds memory limit per test 512 megabytes inpu ...
- VB.NET的前世今生
[前言]初次见到这个强大的东西.一看名字就没有了陌生感,由于它和我曾经见过的VB肯定有非常多的联系. 俗话说,看人看相,了解看感觉(O(∩_∩)O~~几乎相同这个意思吧). 要想了解VB.net就要从 ...
- ios 深入讲解iOS键盘一:控制键盘隐藏显示
在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...