当前位置:网站首页>Leetcode 1209. 删除字符串中的所有相邻重复项 II(牛逼,终于过了)
Leetcode 1209. 删除字符串中的所有相邻重复项 II(牛逼,终于过了)
2022-06-23 11:31:00 【我不是萧海哇~~~~】

给你一个字符串 s,「k 倍重复项删除操作」将会从 s 中选择 k 个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。
你需要对 s 重复进行无限次这样的删除操作,直到无法继续为止。
在执行完所有删除操作后,返回最终得到的字符串。
本题答案保证唯一。
示例 1:
输入:s = "abcd", k = 2
输出:"abcd"
解释:没有要删除的内容。
示例 2:
输入:s = "deeedbbcccbdaa", k = 3
输出:"aa"
解释:
先删除 "eee" 和 "ccc",得到 "ddbbbdaa"
再删除 "bbb",得到 "dddaa"
最后删除 "ddd",得到 "aa"
示例 3:
输入:s = "pbbcggttciiippooaais", k = 2
输出:"ps"
提示:
- 1 <= s.length <= 10^5
- 2 <= k <= 10^4
- s 中只含有小写英文字母。
Code:
class Solution {
public:
string removeDuplicates(string s, int k) {
string res;
int size=s.length();
int num=0;
for(int i=0;i<size;i++)
{
if(res.back()!=s[i])
{
res.push_back(s[i]);
num=1;
continue;
}
else
{
num++;
res.push_back(s[i]);
if(num==k)
{
for(int j=0;j<k;j++)
{
res.pop_back();
}
num=1;
for(int j=res.size()-1;j>=0;j--)
{
if((j-1)>=0)
{
if(res[j]==res[j-1])
num++;
else
break;
}
}
}
}
}
return res;
}
};
边栏推荐
- Monitor the cache update of Eureka client
- How to implement a distributed lock with redis
- Win10 微软输入法(微软拼音) 不显示 选字栏(无法选字) 解决方法
- CIFAR公开第二阶段泛加拿大AI战略
- 坚持五件事,带你走出迷茫困境!
- What are the top ten securities companies in China? Is it safe to open a mobile account?
- How many days is the general term of financial products?
- One picture decoding opencloudos community open day
- 今天14:00 | 12位一作华人学者开启 ICLR 2022
- After repeated pressure, Apple may significantly increase the price of iphone14
猜你喜欢
[Architect (Part 40)] connecting mongodb database developed by server

汉源高科新一代绿色节能以太网接入工业交换机高效节能型千兆工业以太网交换机

Vone新闻 | 旺链科技赋能众享链网自组织管理,打造企业级联盟DAO

Over a year, time has changed. Chinese chips have made breakthroughs, but American chips are difficult to sell

【ML】QuantileRegressor

L'outil de périphérique deveco aide au développement de périphériques openharmony

After repeated pressure, Apple may significantly increase the price of iphone14

16路HD-SDI光端机多路HD-SDI高清视频光端机16路3G-SDI高清音视频光端机

汉源高科USB3.0光端机USB工业触摸屏光端机USB3.0光纤延长器USB3.0光纤传输器

1路百兆光纤收发器1百兆光1百兆电桌面式以太网光纤收发器内置电源
随机推荐
ESP32-C3入门教程 问题篇⑦—— fatal error: esp_bt.h: No such file or directory 找不到esp_bt.h
强化责任意识和底线思维 全力筑牢抗洪抢险“安全堤”
有没有碰到过flink1.15.0 和 flink-cdc-mysql 2.2.1不兼容的情况?f
使用单调栈解题
在工作中学习的三个方法
4路电话+1路千兆以太网4路PCM电话光端机
Tamidog | analysis of investor types and enterprise investment types
惊!AMD 350亿美元收购赛灵思!
Runtime application self-protection (rasp): self-cultivation of application security
攻防演练合集 | 3个阶段,4大要点,蓝队防守全流程纲要解读
After repeated pressure, Apple may significantly increase the price of iphone14
On the structure of annotation platform
Signature analysis of app x-zse-96 in a Q & a community
1154. 一年中的第几天
网上注册股票开户很困难么?现在网上开户安全么?
Deveco device tool helps openharmony device development
[use of Matlab graphical user interface controls]
DevEco Device Tool 助力OpenHarmony设备开发
The simplest DIY actuator controller based on 51 single chip microcomputer
如何用 Redis 实现一个分布式锁