当前位置:网站首页>栈与队列——1047. 删除字符串中的所有相邻重复项
栈与队列——1047. 删除字符串中的所有相邻重复项
2022-07-24 15:55:00 【向着百万年薪努力的小赵】
1 题目描述
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
2 题目示例
输入:“abbaca”
输出:“ca”
解释:
例如,在 “abbaca” 中,我们可以删除 “bb” 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 “aaca”,其中又只有 “aa” 可以执行重复项删除操作,所以最后的字符串为 “ca”。
3 题目提示
1 <= S.length <= 20000
S 仅由小写英文字母组成。
4 思路
充分理解题意后,我们可以发现,当字符串中同时有多组相邻重复项时,我们无论是先删除哪一个,都不会影响最终的结果。因此我们可以从左向右顺次处理该字符串。
而消除—对相邻重复项可能会导致新的相邻重复项出现,如从字符串abba 中删除bb会导致出现新的相邻重复项aa出现。因此我们需要保存当前还未被删除的字符。一种显而易见的数据结构呼之欲出:栈。我们只需要遍历该字符串,如果当前字符和栈顶字符相同,我们就贪心地将其消去,否则就将其入栈即可。
复杂度分析
·时间复杂度:O(n),其中n是字符串的长度。我们只需要遍历该字符串一次。
空间复杂度:O(n)或 o(1),取决于使用的语言提供的字符串类是否提供了类似「入栈」和「出栈」的接口。注意返回值不计入空间复杂度。
5 我的答案
class Solution {
public String removeDuplicates(String s) {
StringBuffer stack = new StringBuffer();
int top = -1;
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (top >= 0 && stack.charAt(top) == ch) {
stack.deleteCharAt(top);
--top;
} else {
stack.append(ch);
++top;
}
}
return stack.toString();
}
}
边栏推荐
- faster-rcnn 训练自己的数据集
- SQL row to column, column to row
- 【tf.keras】:版本从1.x升级到2.x遇到的一个问题:InvalidArgumentError: Cannot assign a device for operation embedding_
- 请问好的券商的排名?网上开户安全吗
- MATLAB image defogging technology GUI interface - global balance histogram
- Azure key vault (1) Introduction
- Vscode common shortcut keys
- JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
- Personal practical experience: Data Modeling "whether account data belongs to dimension or account domain"
- Getting started with OpenMP
猜你喜欢

Nine key measures to maintain server security in Hong Kong
![Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]](/img/9c/05118efe2152dc5461a92720732076.jpg)
Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]

未来数据库需要关心的硬核创新

How to deal with being attacked? Advanced anti DDoS IP protection strategy

Fast RCNN trains its own data set

22 bracket generation

AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘

JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue

Using JS to implement click events

Fine tune layoutlm V3 for bill data processing and content recognition
随机推荐
PHP中array_merge的坑
若依 this.$router.push 同地址不同参,页面不刷新问题
After taking aiyouteng's medicine, Naifei's condition improved
Lsyncd 实时同步
253 Conference Room II
2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)
【LeetCode】Day103-搜索二维矩阵 II
15. Talk about these common multi-threaded interview questions
狗牙根植物介绍
Error 1053: the service did not respond to the start or control request in a timely fashion
Parse string
R language Visual facet chart, multivariable grouping nested multilevel t-test, and specify reference level, visual multivariable grouping nested multilevel faceting boxplot, and add significance leve
AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
Introduction to single chip microcomputer: LED lights cycle to the left and turn on
Is it safe for Anxin securities to open an account on mobile phone?
Varnish4.0 cache agent configuration
【tf.keras】:版本从1.x升级到2.x遇到的一个问题:InvalidArgumentError: Cannot assign a device for operation embedding_
kubernetes GPU的困境和破局
MySQL learning notes (summary)
机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤