当前位置:网站首页>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;
}
};
边栏推荐
- 【ML】QuantileRegressor
- Simplest DIY steel patriot machine gun controller based on Bluetooth, 51 MCU and steering gear
- Creating neural networks using tensorflow2
- php 手写一个完美的守护进程
- 我在佛山,到哪里开户比较好?手机开户安全么?
- 坚持五件事,带你走出迷茫困境!
- From 0 to 1, how does the IDE improve the efficiency of end-to-end R & D| DX R & D mode
- ESP32-C3入门教程 问题篇⑧——blufi_example.c:244: undefined reference to `esp_ble_gap_start_advertising
- Design and implementation of distribution network and Internet connection scheme for esp32-cam high cost performance temperature and humidity monitoring system
- 安卓安全/逆向面试题
猜你喜欢

64路PCM电话光端机64路电话+2路百兆以太网电话光端机64路电话PCM语音光端机
Go zero micro Service Practice Series (VI. cache consistency assurance)

六张图详解LinkedList 源码解析

32路电话+2路千兆以太网32路PCM电话光端机支持FXO口FXS语音电话转光纤

Vone news | wanglian technology empowers the public to enjoy the self-organization management of the chain network, creating an enterprise level alliance Dao

"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu

1路百兆光纤收发器1百兆光1百兆电桌面式以太网光纤收发器内置电源

php 手写一个完美的守护进程

使用Mycat进行MySQL单库分表

At 14:00 today, 12 Chinese scholars started ICLR 2022
随机推荐
Runtime application self-protection (rasp): self-cultivation of application security
浅谈标注平台架构
使用单调栈解题
使用tensorflow2创建神经网络
OpenHarmony应用开发【01】
如何用 Redis 实现一个分布式锁
你真的理解LDO的輸出電容嗎!?
Esp32-cam, esp8266, WiFi, Bluetooth, MCU, hotspot create embedded DNS server
分享一个手游脚本源码
Video data annotation tools and platforms (data annotation company)
DevEco Device Tool 助力OpenHarmony设备开发
如何在 FlowUs、Notion 等笔记软件中使用间隔重复记忆系统?
Force buckle 1319 Number of connected network operations
强化责任意识和底线思维 全力筑牢抗洪抢险“安全堤”
Deep dive kotlin synergy (XIV): problems of shared state
Win10 wireless network. If the system cannot search WLAN, the solution (and VMnet1, 8)
运行时应用自我保护(RASP):应用安全的自我修养
华为云如何实现实时音视频全球低时延网络架构
4E1 PDH光端机19英寸机架式单纤传输20km E1接口光纤网络光端机
汉源高科8路电话+1路百兆以太网RJ11电话光端机 8路PCM电话光端机