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. FileDb

    filedb FileDB - A C# database to store files FileDB is a free, fast, lightweight C# (v3.5) DLL proje ...

  2. SignalR 行实时通信遇到的

    SignalR可用于向ASP.NET应用程序添加任何类型的“实时”Web功能.虽然聊天经常被用作示例,但您可以做更多的事情.每当用户刷新网页以查看新数据,或者页面实现Ajax长轮询以检索新数据时,都可 ...

  3. java8_api_日期时间

    日期时间处理    Date类,其中很多方法已经不用了    Calendar类,java.util包中的抽象类        Date类,其对象代表即时时间,存储的是从19700101000000距 ...

  4. 将centos的yum源修改为阿里云的yum源

    CentOS系统更换软件安装源 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentO ...

  5. install chrome and chrome driver on ubuntu

    sudo apt install python-minimal # python 2.7.xsudo apt install python-pip # https://www.ubuntuupdate ...

  6. GridView更新后获取不到文本框修改后的值

    需要在Page_Load事件里为gridview绑定数据时,添加回传判断 if (!IsPostBack) { 绑定数据 }

  7. ActiveMQ(为什么要使用消息中间件,JMS传输模型)

    为什么要使用消息中间件:    同步请求:当客户端向服务器发送一条请求的时候,此时服务器由于网络,或者处理一些比较大的数据的时候,可能有延迟,客户端 会处于一直等待的状态.只有等待服务器返回处理结果, ...

  8. 将项目打成jar包执行 在liunx上执行 java -xx.jar

    一:普通maven java项目 项目目录 pom.xml <?xml version="1.0" encoding="UTF-8"?> <p ...

  9. 使用pgrouting进行最短路径搜索

       PgRouting是基于开源空间数据库PostGIS用于网络分析的扩展模块,最初它被称作pgDijkstra,因为它只是利用Dijkstra算法实现最短路径搜索,之后慢慢添加了其他的路径分析算法 ...

  10. 一台机器部署多个tomcat服务 nginx反向代理多个服务 笔记

    安装tomcat步骤           1. 下载apache-tomcat-8.0.30 ,下载下来的文件为apache-tomcat-8.0.30-windows-x64.zip         ...