LeetCode 15 3Sum [sort] <c++>

给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合。

C++

先自己写了一发,虽然过了,但跑了308 ms...

我的做法是先排序,扫一遍,处理出unorder_map<0-a[i],i>的hash表。再\(O(n^2)\)枚举前两个元素,查表直接知道第三个元素的位置,然后将三元组加入到答案集合中,通过枚举时添加的限制条件规避重复元素。

复杂度\(O(n^2)\),由于使用了hash表,常数较大。

class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
std::vector<std::vector<int> > res;
if(nums.size()<3) return res;
std::sort(nums.begin(),nums.end());
const int target = 0;
std::unordered_map<int,int> ump;
for(auto i=nums.begin();i!=nums.end();i++)
ump[target-*i] = std::distance(nums.begin(),i)+1;
for(auto i=nums.begin();i<nums.end();i++){
if(i>nums.begin() && *i == *(i-1)) continue;
for(auto j=i+1;j<nums.end();j++){
if((j>i+1 && *j== *(j-1))|| ump[*i+*j]-1<=std::distance(nums.begin(),j))continue;
res.push_back({*i,*j,*(nums.begin()+ump[*i+*j]-1)});
}
}
return res;
}
};

学习一发没有常数的\(O(n^2)\),只用了64 ms

排序后,枚举第一个元素,左右指针夹逼(根据枚举元素+左指针元素+右指针元素与0的大小关系,判断移动哪一个指针,移动方向一定是使左右指针所夹范围更小的)

class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
std::vector<std::vector<int> > res;
if(nums.size()<3) return res;
std::sort(nums.begin(),nums.end()); // 先排序
const int target = 0;
auto last = nums.end();
for(auto i = nums.begin();i<last-2;i++){
auto j = i+1; // 左指针初始化
if(i>nums.begin()&&*i == *(i-1)) continue; // 避免枚举重复元素
auto k = last - 1; // 右指针初始化
while (j < k){
if(*i+*j+*k<target){ // 左指针右移,使和变大
j++;
while (*j==*(j-1)) j++; // 跳过重复元素
}
else if(*i+*j+*k>target){ // 右指针左移,使和变小
k--;
while (*k==*(k+1)) k--; // 跳过重复元素
}
else{ // 加入答案集合
res.push_back({*i,*j,*k});
j++;
k--;
while (*j==*(j-1) && *k==*(k+1) && j<k) j++; // 跳过重复元素
}
}
}
return res;
}
};

LeetCode 15 3Sum [sort] <c++>的更多相关文章

  1. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  2. [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 ...

  3. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  4. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

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

  6. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  7. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  8. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  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. kalilinux渗透测试笔记

    声明:本文理论大部分是苑房弘kalilinux渗透测试的内容 第五章:基本工具 克隆网页,把gitbook的书记下载到本地 httrack "http://www.mybatis.org/m ...

  2. 算法——八皇后问题(eight queen puzzle)之回溯法求解

    八皇后谜题是经典的一个问题,其解法一共有种! 其定义: 首先定义一个8*8的棋盘 我们有八个皇后在手里,目的是把八个都放在棋盘中 位于皇后的水平和垂直方向的棋格不能有其他皇后 位于皇后的斜对角线上的棋 ...

  3. 如何配置adb环境变量

    如何配置adb环境变量? 1.我的电脑---控制面板---高级系统设置 2.点击[高级系统设置],弹出系统属性的弹框, 3.点击[环境变量],弹出环境变量弹框,新建一个系统变量,命名为Android ...

  4. vue-cli项目 build后请求本地static文件中的 json数据,路径不对,报错404处理方法

    vue-cli 项目 build  出错点: 1,build生成dist 放在tomcat上 报错,不显示内容  解决办法: config>index.js===>assetsPublic ...

  5. 强连通分量Kosaraju

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

  6. 15 个 Eclipse 常用开发快捷键使用技巧

    15 个 Eclipse 常用开发快捷键使用技巧 1.alt+? 或 alt+/:自动补全代码或者提示代码 2.ctrl+o:快速outline视图 3.ctrl+shift+r:打开资源列表 4.c ...

  7. 烽火2640路由器命令行手册-11-IP语音配置命令

    IP语音配置命令 目  录 第1章 配置拨号对命令... 1 1.1 配置拨号对命令... 1 1.1.1 dial-peer voice. 1 1.1.2 application. 2 1.1.3 ...

  8. RN和IOS原生端交互

    1.RCTBridgeModule #import <Foundation/Foundation.h> #import "RCTBridgeModule.h" #imp ...

  9. Python数据可视化之Matplotlib实现各种图表

    数据分析就是将数据以各种图表的形式展现给领导,供领导做决策用,因此熟练掌握饼图.柱状图.线图等图表制作是一个数据分析师必备的技能.Python有两个比较出色的图表制作框架,分别是Matplotlib和 ...

  10. 关于spring aop Advisor排序问题

    关于spring aop Advisor排序问题 当我们使用多个Advisor的时候有时候需要排序,这时候可以用注解org.springframework.core.annotation.Order或 ...