3sum 求三数之和等于0,不允许重复
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,不允许重复的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
		Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ... 
- [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 < ... 
- 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 ... 
- 【LeetCode】15、三数之和为0
		题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ... 
- 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 ... 
- 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 ... 
- 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 ... 
- [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 ... 
- [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 ... 
随机推荐
- Linux df -h空间显示不正确
			今天发现一个测试数据库磁盘空间快满了,准备将几个不再用的表空间删除.通过以下命令删除表空间内容及数据文件. drop tablespace tablespace_name including cont ... 
- 使用 AppScan 进行扫描
			针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Analysis and Action ... 
- winscp工具和xshell连接linux机器时切换到root账户
			由于工作中一些机器不能以root直接登陆(sshd_config配置了不能直接root登陆),但是又想连接的时候切换为root用户 处理方式 1.给普通用户sudo su - 权限 命令行输入visu ... 
- day01知识点
			1.计算机基础 2.Python的历史 3.编码语言分类 Python是一门动态解释性的强制类型定义语言 4.Python的解释器种类 5.变量 法律规则:字母,数字,下划线(数字不能 ... 
- 什么是HDR?
			参考:https://baijiahao.baidu.com/s?id=1606763887374415267&wfr=spider&for=pc HDR——即高动态范围图像(High ... 
- RN 的页面布局
			从 https://blog.csdn.net/liangzelei/article/details/53965417转载 React Native布局详细指南 https://www.jiansh ... 
- 工控随笔_17_西门子_WinCC的VBS脚本_06_过程和函数
			和其他语言一样,vbs提供了过程和函数机制,通过函数和过程可以优化代码结构和实现代码复用, 减少代码的编写量. 一.代码 具体不再多说,看实例代码. ' vbs的函数和过程 '1.过程式编程是一大编程 ... 
- Spring中的接口BeanFactory和FactoryBean的学习
			BeanFactory: 相当于对象工厂,可以获取对象的实例以及相应的属性.BeanFactory定义了IOC容器的最基本形式,并提供了IOC容器应遵守的的最基本的接口,也就是Spring IOC所遵 ... 
- sed命令的基本使用方法
			sed命令 stream editor,用程序的方式编辑文本.基本上是玩正则模式匹配. 用s命令替换 $ sed "s/my/Hao Chen's/g" pets.txt 单引号去 ... 
- python爬虫学习笔记(二)——基础篇之爬虫基本原理
			1.什么是爬虫? 请求网站并提取数据的自动化程序 2.爬虫基本流程 2.1发起请求 通过HTTP库向目标站点发起请求,即发起一个Request,请求可以包含额外的headers等信息,等待服务器响应: ... 
