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. Redis 简介与命令操作

    redis 是 key-value 的数据,所以每个数据都是一个键值对,键的类型是字符串: 值的类型分为五种:string.hash.list.set(集合).zset(有序集合). 数据操作的全部命 ...

  2. HTML5+CSS3-学习总结

    这是第三次学标签和样式了,虽然距离上次差不多2年.可学过的东西依旧还在. 体会 1.  相对于前端技术,基础还是很重要的. 2.  虽然很繁多.并不是杂乱无章的. 3.  HTML5在新增的几个标签, ...

  3. SpringMVC使用StandardServletMultipartResolver上传文件

    DispatcherServlet并没有实现任何解析multipart请求数据的功能,它将该任务委托给了Spring中MultipartResolver策略接口的实现,通过该接口的实现类来解析mult ...

  4. 记一次简单的GetShell案例

    案例链接: http://202.112.51.184:8007/ 打开链接,发现分了多个页面: 挨个点击,大概清楚是上传指定格式的文件然后在搜索的时候使文件执行从而GetShell,观察发现点击每个 ...

  5. 记录一个nginx的配置

    rt #user xiaoju; worker_processes ; #error_log logs/error.log notice; #error_log logs/error.log debu ...

  6. Mongodb 安装错误汇总

    Failed to restart mongod.service: Unit mongod.service not found. 解决方法: Most probably unit mongodb.se ...

  7. SQLAlchemy+Flask-RESTful使用(一)

    前言 开新坑啦.最近打算自己开一个资源聚合网站.就用Flask. 当然也使用了 Flask-RESTful和SQLAlchemy啦 写的过程中遇到过很多坑/觉得比较有意义的就写在这里. 变更记录 # ...

  8. python设置虚拟环境

    一.介绍 虚拟环境-virtualenv是一个用于隔绝的python环境的工具,虚拟环境与本地环境互相隔离,互不影响,例如我们要安装django2.0的版本但是本地已经安装了1.1的,这时只需要在本地 ...

  9. Android 插件化和热修复知识梳理

    概述 在Android开发中,插件化和热修复的话题越来越多的被大家提及,同时随着技术的迭代,各种框架的发展更新,插件化和热修复的框架似乎已经日趋成熟,许多开发者也把这两项技术运用到实际开发协作和正式的 ...

  10. Django组件-cookie与session

    一.会话跟踪技术 1.什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而 ...