当前位置:网站首页>leetcode刷题:栈与队列04(删除字符串中的所有相邻重复项)
leetcode刷题:栈与队列04(删除字符串中的所有相邻重复项)
2022-07-01 19:16:00 【涛涛英语学不进去】
1047. 删除字符串中的所有相邻重复项
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
示例:
- 输入:“abbaca”
- 输出:“ca”
- 解释:例如,在 “abbaca” 中,我们可以删除 “bb” 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 “aaca”,其中又只有 “aa” 可以执行重复项删除操作,所以最后的字符串为 “ca”。
提示:
- 1 <= S.length <= 20000
- S 仅由小写英文字母组成。
把元素逆序入栈,入的时候判断一下和栈顶元素是否相同,相同则不入,反而出栈。最后遍历栈获取结果。逆逆得正,这时候就是想要的结果了。感觉双指针做更简单。我试试。
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName RemoveDuplicates * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 18:04 * @Version 1.0 * https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/ * 1047. 删除字符串中的所有相邻重复项 **/
public class RemoveDuplicates {
/** * 这题双指针法能做,栈也能做 * * @param s * @return */
public static String removeDuplicates(String s) {
String result = "";
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack();
for (int i = chars.length - 1; i >= 0; i--) {
//无元素时直接入栈
if (stack.size() == 0) {
stack.push(chars[i]);
} else if (stack.peek() != chars[i]) {
//如果当前元素和栈顶元素不同,入栈
stack.push(chars[i]);
} else {
stack.pop();
}
}
while (stack.size() != 0) {
result += stack.pop();
}
return result;
}
public static void main(String[] args) {
System.out.println(removeDuplicates("abbaca"));
}
}
自己尝试写双指针写法失败
char[] chars = s.toCharArray();
int right = 0;
int left = 0;
while (right < s.length()) {
chars[left] = chars[right];
if (left > 0 && chars[left] == chars[left - 1]) {
left--;
} else {
left++;
}
right++;
}
return new String(chars, 0, left);
类似三数之和的写法,左边相对固定,右边向右滑动。
边栏推荐
- Using qeventloop to realize synchronous waiting for the return of slot function
- Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
- How to prevent repeated submission of new orders
- 强大的万年历微信小程序源码-支持多做流量主模式
- 关联线探究,如何连接流程图的两个节点
- 由浅入深学会白盒测试用例设计
- Common components of flask
- PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
- 漏洞复现-.Net-ueditor上传
- Solve the problem of slow or failed vscode download
猜你喜欢
fastDFS入门
RichView TRVDocParameters 页面参数设置
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?
多个张量与多个卷积核做卷积运算的输出结果
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
RichView RichEdit SRichViewEdit PageSize 页面设置与同步
Détection des cibles - série Yolo
Learn white box test case design from simple to deep
Practical project notes (I) -- creation of virtual machine
随机推荐
ORA-01950
Face recognition system opencv face detection
A quietly rising domestic software, low-key and powerful!
【let var const】
Practical project notes (I) -- creation of virtual machine
RichView RichEdit SRichViewEdit PageSize 页面设置与同步
internship:逐渐迈向项目开发
Win11 how to hide the taskbar? Win11 method to hide the taskbar
PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
利用QEventLoop实现同步等待槽函数返回
Win11怎么关闭开机自启动软件
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
Flask 常用组件
How to prevent repeated submission of new orders
ORA-01950
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
图片拼图微信小程序源码_支持多模板制作和流量主
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
How to create a pyramid with openmesh
Past and present life of product modular design