LeetCode 15 3Sum [sort] <c++>
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++>的更多相关文章
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [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. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=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. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 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 ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 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 ...
随机推荐
- pythonのdjango Form简单应用。
Form表单有两种应用场景: 1.生成HTML标签. 2.验证输入内容. 如果我们在django程序中使用form时,需要在views中导入form模块 from django import form ...
- 「JavaScript面向对象编程指南」对象
对象的属性名可加上引号,下面三行代码所定义的内容是完全相同的 var hero = { occupation : 1 }; var hero = { "occupation" : ...
- git知识总结-4.git服务器搭建及迁移git仓库
1. 前言 因为手里有一份代码之前是直接从其它git服务器上克隆下来的,现在想自己搭建一个git服务器把这份代码管起来. 2. 搭建git服务器 1.安装git: $ sudo apt-get ins ...
- 【easy】198. House Robber 123总结……
题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...
- C++中public/protect/private三种访问权限控制
一.成员访问权限控制 1.public (1)public成员变量可以被成员函数访问 [访问性] (2)public成员可以被实体对象访问 [访问性] (3)public成员可以成为子类成员 [ ...
- springboot 中页面跳转问题:window.location.href
我的一个HTML页面 点击注册 本该到注册页面,但是却一直跳到同目录的一个Error.html文件夹下 该页面: 删掉Error.html还不行:会报错,而且改变window.location.hre ...
- 安装kafka过程及出现的问题解决
第一步:下载kafka安装包 下载地址:http://kafka.apache.org/downloads 解压 到/usr/local 目录 tar -zxvf kafka_2.12-2.2.0 第 ...
- netty 服务器端流程调度Flow笔记
create NioEventLoopGroup Instance 一.NioServerSocketChannel init note:Initializing ChannelConfig crea ...
- CTO 能力模型(简化版)
最近思考了很多,我在大贲这几年的工作内容.从一开始到现在,伴随着大贲从一二十人,走到了现在的两百多人.我的工作也从一开始的带头冲锋陷阵,逐步转移到了带领产品研发,再到后来的全公司多业务线的技术管理工作 ...
- SpringBoot的事件监听
事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...