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. idea编译错误提示编译版本不对,需要注意的配置

  2. 吴恩达《机器学习》编程作业——machine-learning-ex1:线性回归

    ❄❄❄❄❄❄❄❄[回到目录]❄❄❄❄❄❄❄❄ 本次编程作业中,需要完成的代码有如下几部分: [⋆] warmUpExercise.m - Simple example function in Octa ...

  3. Unity AssetBundle的生成、加载和热更新

    当前使用的是unity2018.2.6版本. 生成AssetBundle 这个版本生成AssetBundle有两种方式,一种是在资源的Inspector面板下边配置AssetBundle名称,然后调用 ...

  4. msdn原版系统和原版office

    建议使用迅雷下载工具进行下载 激活详见:在线激活win10.win8/8.1和office2019.2016.2013等的kms激活工具 windows 10 家庭版/家庭单语言版/专业版/教育版/专 ...

  5. TableView+Button

    local MainScene = class("MainScene", cc.load("mvc").ViewBase) function MainScene ...

  6. 5G到来,App的未来,是JavaScript,Flutter还是Native ?

    Native App React Native(RN)发布于2015年,也是使用JavaScript语言进行跨平台APP的开发.与H5开发不同的是,它使用JS桥接技术在运行时编译成各个平台的Nativ ...

  7. 设计模式 — 工厂方法模式(Factory Method)

    在开发系统中,经常会碰到一个问题.现在需要实现的一些功能,但是这个功能模块以后一定是需要扩展的,那么现在开发中就不仅要实现现在的功能,还要考虑以后的扩展.那么为了系统的健壮,扩展就要遵循开闭原则(简单 ...

  8. python django2.x报错No module named 'django.core.urlresolvers'

    解决方法就是: from django.urls import reverse 最近从django1.9迁移到django2.0中出现一个意外的报错: 这个报错的原因在stack overflow上有 ...

  9. 饮冰三年-人工智能-Python-30 python开发中常见的错误

    1:触发条件:创建的实体类生成到数据库表时报错 报错信息:TypeError: __init__() missing 1 required positional argument: 'on_delet ...

  10. 微服务(Microservices)【翻译】

    微服务 “微服务架构(Microservice Architecture)”一词在过去几年里广泛的传播,它用于描述一种设计应用程序的特别方式,作为一套独立可部署的服务.目前,这种架构方式还没有准确的定 ...