https://leetcode.com/problems/3sum/

套路比较常见了,最重要的是去重。还是没法一次通过。

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& a) {
vector<vector<int>> ans;
int n = a.size();
if(n < ) return ans; sort(a.begin(),a.end()); for(int i = ; i < n - ; i++)
{
int target = - a[i]; for(int j = i + , k = n - ; j < k; )
{
int sum2 = a[j] + a[k]; if(sum2 == target)
{
vector<int> tmp{a[i],a[j],a[k]};
ans.push_back(tmp); // 这一行做完以后,a[j]依然等于a[j-1]。
while(j+ < n && a[j+]==a[j]) j++;
j++;
while(k- >= && a[k-]==a[k]) k--;
k--;
}
else if(sum2 < target)
{
j++;
}
else
{
k--;
}
}
//这一步并不总是执行的,只是走到所有的重复的最后。
while(i+ < n && a[i+] == a[i]) i++;
} return ans;
}
}; 去重的原理要好好想一下:
比如 -2 -2 -2 0 1 1 1 1 2
第一次i选中-2的时候,后面的组合有0 2,1 1. 这之后,如果再选第二个-2作为第一个数的话,后面依然会产生0 2 , 1 1,就发生了重复。
因为,如果第一次在集合A中找2,第二次相当于在A的子集中找2。第二次找出的结果肯定是第一次结果的子集。
所以,第一次选-2以后,第二次选应该找到第一个不是-2的数。这样去重。

3sum 求三数之和等于0,不允许重复的更多相关文章

  1. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  2. [LeetCode] 259. 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  3. LeetCode 15. 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 ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  6. 15. 3Sum[M]三数之和

    题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...

  7. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  8. [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 un ...

  9. [LeetCode] 15. 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 ...

随机推荐

  1. 3.GUI Skin和自定义风格的组件 --《UNITY 3D 游戏开发》笔记

    自定义皮肤还是很受女孩子欢迎的吧,这样操作一下界面是不是就可以变得美美哒了~ 先pick一下测试代码: public class GUISkinScript : MonoBehaviour { //自 ...

  2. 插入排序-C#实现

    插入排序包括:直接插入排序和希尔排序. 具体代码如下: 直接插入排序: /// <summary> /// 直接插入排序 /// 适用于少量元素的排序 /// 稳定性:稳定 /// 时间复 ...

  3. Int和String互转的方法

    Java 中int.String的类型转换   int -> String int i=12345;String s="";第一种方法:s=i+"";第二 ...

  4. 1.1.26 word内容导入PPT

    1.在开始菜单栏选择[视图]>[大纲].进入大纲后,对文本设置大纲级别. 2.设置好后,在[word选项]>下拉菜单中找到[不在功能区命令]>选择[发送到PPT].

  5. httpd2.4.6配置文件解释说明

    本文httpd版本为:2.4.6 ServerRoot 先来看一下httpd.conf配置文件中的ServerRoot默认定义: # cat /etc/httpd/conf/httpd.conf |e ...

  6. java 生成微信的二维码 工具类

    package com.app.wii.util; import java.io.File;import java.io.FileInputStream;import java.io.FileOutp ...

  7. SSHD启动失败,错误码255

    查看/etc/ssh/sshd_config 发现,Listen Address并不是我想要的ip,将其注释掉 sshd restart,结果返回 Permission denied (publick ...

  8. 批处理判断是BIOS还是UEFI启动

    https://files.cnblogs.com/files/liuzhaoyzz/detectefi.rar @echo offpushd %~dp0reg add "HKEY_CURR ...

  9. 51nod 1162 质因子分解

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1162 数据范围大约是2^97,需要高精度计算 可以使用pollard- ...

  10. [UE4]让箭头保持水平的第二种方法:Combinrotators、Delta(Rotator)

    一.手柄在世界坐标系中有一个绝对朝向,我们可以知道箭头相对于手柄的朝向,相对于手柄的旋转角度. 可以通过手柄绝对朝向.箭头的相对于手柄的朝向计算得到箭头的绝对朝向. 在得到箭头的相对于手柄的角度,在这 ...