当前位置:网站首页>LeetCode659.分割数组为连续子序列 (哈希表)
LeetCode659.分割数组为连续子序列 (哈希表)
2022-07-26 14:35:00 【Jay_fearless】
题目描述
给你一个按升序排序的整数数组 num(可能包含重复数字),请你将它们分割成一个或多个子序列,其中每个子序列都由连续整数组成且长度至少为 3 。
一个子序列是从原始数组挑选一部分(也可以全部)元素而不改变相对位置形成的新数组
如果可以完成上述分割,则返回 true ;否则,返回 false 。
说明:
输入的数组长度范围为 [1, 10000]
输入样例1
8
1 2 3 3 4 4 5 5
输出样例1
true
解释:
你可以分割出这样两个连续子序列 :
1, 2, 3
3, 4, 5
输入样例2
6
1 2 3 4 4 5
输出样例2
false
分析
本题目采取贪心的思路,我们首先用一个哈希表mp1记录所有数字出现的次数,之后用另外一个哈希表mp2来表示长度至少为3的序列的是否存在。
如果当前数为x,如果mp2[x-1]存在,就把这个数接到x-1后面,同时更新mp2中以x-1结尾和以x结尾的序列个数。
若x-1的序列不存在,就重新开一个序列,但需要判断x+1,x+2是否存在。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
int n;
class Solution {
public:
bool isPossible(vector<int>& nums) {
map<int,int> mp1,mp2;
for(auto x:nums) mp1[x]++; //记录所有数字的出现次数
for(auto x:nums)
{
if(!mp1[x]) continue; //如果这个数用完了,就跳过本轮循环
if(mp2[x-1]){ //如果以x-1的序列存在,就把x加到末尾
mp2[x]++;
mp2[x-1]--;
mp1[x]--;
}
else if(mp1[x+1] && mp1[x+2]) //若x-1的序列不存在,就重新开一个序列,但需要判断x+1,x+2是否存在
{
mp2[x+2]++; //创建一个以x开头,x+2结尾的序列
mp1[x]--,mp1[x+1]--,mp1[x+2]--;
}
else return false;
}
return true;
}
};
int main()
{
cin>>n;
vector<int> v;
int a;
for(int i=0;i<n;i++)
{
cin>>a;
v.push_back(a);
}
if(Solution().isPossible(v)) puts("true");
else puts("false");
return 0;
}
边栏推荐
- How to do app upgrade test?
- postman 环境变量设置代码存放
- C# 常用功能整合
- JS wave animation effect menu style
- c# 用移位 >> 和运算与 &判断两个 二进制数 是否发生过改变
- 【文件上传漏洞-06】分布式配置文件攻击实验—以upload-labs-4为例
- 全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
- Keyboard shortcut to operate the computer (I won't encounter it myself)
- WPF common function integration
- 请问数据库规范的文档吗 参考一下?
猜你喜欢

C common function integration

【2022国赛模拟】白楼剑——SAM、回滚莫队、二次离线

【愚公系列】2022年7月 Go教学课程 017-分支结构之IF
![[1.2. return and risk of investment]](/img/61/0135c429225e1c18705749a20e2a96.png)
[1.2. return and risk of investment]

What is the problem of the time series database that has been developed for 5 years?

14. Bridge-Based Active Domain Adaptation for Aspect Term Extraction 阅读笔记
![[integer programming]](/img/e5/aebc5673903f932030120822e4331b.png)
[integer programming]

Low power multi-channel wfas1431 wireless data acquisition and transmission instrument operation process description

PyTorch的简单实现

如何评价测试质量?
随机推荐
As the "first city" in Central China, Changsha's "talent attraction" has changed from competition to leadership
Realize the full link grayscale based on Apache APIs IX through MSE
Usage of nn.conv2d and nn.convtranspose2d functions in pytorch
【整数规划】
VBA upload pictures
Kubernetes----Pod配置资源配额
31. Opinion-based Relational Pivoting forCross-domain Aspect Term Extraction 阅读笔记
【愚公系列】2022年7月 Go教学课程 017-分支结构之IF
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
median filter
SiamFC:用于目标跟踪的全卷积孪生网络
SSH that must be read on cloud native
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
Kubernetes ---- pod configuration resource quota
Error reported by Nacos enabled client
Install dexdump on win10 and remove the shell
Introduction to C language must brush the daily question of the collection of 100 questions (1-20)
10 schemes to ensure interface data security
中部“第一城”,长沙“人才引力”从争先到领先
4 kinds of round head arrangement styles overlay styles