当前位置:网站首页>LeetCode - 1047. Remove all adjacent duplicates in a string
LeetCode - 1047. Remove all adjacent duplicates in a string
2022-08-06 07:54:00 【Peas Erguotou】
题目
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们.
在 S 上反复执行重复项删除操作,直到无法继续删除.
在完成所有重复项删除操作后返回最终的字符串.答案保证唯一.
思路
In fact, it can be regarded as a stack,如果栈顶,the same as the top,Then pop the top and next top of the stack,last on the stack,In fact, it is all the characters of the final result.
代码
class Solution {
public String removeDuplicates(String s) {
// s 仅由小写字母组成
char[] word = s.toCharArray();
int length = s.length();
int top = -1;
for (int i = 0; i < length; ++i)
{
if (top==-1 || word[top]!=word[i])
{
word[++top]=word[i];
}
else
top--;
}
return String.valueOf(word,0,top+1);
}
}
结果

A more ingenious process of using the stack to solve problems,Use a top-of-stack pointer,如果,The character at the current position is not the same as the top of the stack pointer,Then copy the character at the current position to the next position of the top of the stack pointer.
If the character at the current position is the same as the top of the stack pointer,Then skip these two characters,把topThe pointer moves down one position to the left.and rematchtopand the case of the character at the next position.
边栏推荐
- 产品经理专业知识50篇(三)-如何寻找用户增长的根本动因
- Cesium关于Entity中的parent、isShowing、entityCollection和监听事件的探讨
- LeetCode——1047. 删除字符串中的所有相邻重复项
- DescrTab2包,输出SCI级别的描述统计表
- 我在yml里设置了全局映射表前缀,但数据库识别不到
- yum offline installation
- Advanced Programming in UNIX Environment - Chapter 2
- [面试篇]Mysql 索引 BTree 与 B+Tree 的区别
- OpenAtom XuperChain 开源双周报 |2022.7.25-2022.8.5
- 按钮只能点击一次
猜你喜欢

【基于simulink的二阶电路仿真】
![[ CTF ]【天格】战队WriteUp-第六届”蓝帽杯“全国大学生网络安全技能大赛(半决赛)](/img/27/42e6276b54e5d13a04607ccd4119c4.png)
[ CTF ]【天格】战队WriteUp-第六届”蓝帽杯“全国大学生网络安全技能大赛(半决赛)

Unity NavMesh基础自动寻路

机械制造企业如何借助ERP系统,做好生产排产管理?

How to limit command length to bounce shell

2022 第四周全球 AI 模型周报

不会吧,不会吧都2022年了你不会还不知道Jmeter原理吧

CPU Architecture at a Glance

CSDN官方插件

Simulate the realization of strcpy function (including multiple optimization ideas)
随机推荐
自用工具 猴子都会用的unity视频播放器
"Digital reconstruction system, CEO is the first step"
使用aggird组件实现下滑请求分页从而实现无限滚动的效果
【matlab的积分,傅里叶,拉普拉斯变换,信号分析】
不会吧,不会吧都2022年了你不会还不知道Jmeter原理吧
LeetCode——1047. 删除字符串中的所有相邻重复项
The origin of the name, concave language -, and moral
为何面试官总是将你简历上的技术问题问到回答不出来为止?
模拟实现strcpy函数的实现(含多次优化思想)
R语言统计与绘图:生存率的比较
机械制造企业如何借助ERP系统,做好生产排产管理?
《UnityShader入门精要》总结(1)理论篇
按钮只能点击一次
BuuWeb
MySQL数据库的逆向生成实体类,查询等接口xml的脚本
猴子都会用的UNITY圆形滑动自动吸附UI工具
How to ensure the security of NFT from the suspected abolition of the magic core
Use the aggird component to implement sliding request paging to achieve the effect of infinite scrolling
unity 3D物体变换(旋转,缩放)
How to limit command length to bounce shell