当前位置:网站首页>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);

类似三数之和的写法,左边相对固定,右边向右滑动。
边栏推荐
- Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
- Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
- Architect graduation summary
- Win11怎么关闭开机自启动软件
- Win11 how to hide the taskbar? Win11 method to hide the taskbar
- 如何用OpenMesh创建一个四棱锥
- 走进如心小镇,数智化变革连接“未来社区”
- Simple but modern server dashboard dashdot
- 目標檢測——Yolo系列
- Principle of motion capture system
猜你喜欢

编译原理复习笔记

独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作

Arduino Stepper库驱动28BYJ-48步进电机测试程序

合成大西瓜小游戏微信小程序源码/微信游戏小程序源码

Vulnerability recurrence - Net ueeditor upload

王者战力查询改名工具箱小程序源码-带流量主激励广告

随机头像大全,多分类带历史记录微信小程序源码_支持流量主

Develop those things: easycvr platform adds playback address authentication function

优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs

Target detection - Yolo series
随机推荐
深度学习 常见的损失函数
8K HDR!|为 Chromium 实现 HEVC 硬解 - 原理/实测指南
A quietly rising domestic software, low-key and powerful!
关联线探究,如何连接流程图的两个节点
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
cocoaPods 添加成功后,导入不了头文件或者not found file 报错
Problems encountered in installing MySQL in docker Ubuntu container
docker ubuntu容器中安装mysql遇到的问题
Getting started with fastdfs
柒微自动发卡系统源码
How to turn off the boot auto start software in win11
联想电脑怎么连接蓝牙耳机?
PHP gets the external chain address of wechat applet and applet store
图片拼图微信小程序源码_支持多模板制作和流量主
升级版手机检测微信工具小程序源码-支持多种流量主模式
On the next generation entrance of the metauniverse -- the implementation of brain computer interface
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
深度学习 神经网络基础
ORA-01950