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

类似三数之和的写法,左边相对固定,右边向右滑动。
边栏推荐
- Arduino Stepper库驱动28BYJ-48步进电机测试程序
- 人脸识别系统 —— OpenCV人脸检测
- What did you learn about cheating after you went to college?
- 升级版手机检测微信工具小程序源码-支持多种流量主模式
- Slf4j打印异常的堆栈信息
- Face recognition system opencv face detection
- Iframe 父子页面通信
- Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
- 优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs
- 运动捕捉系统原理
猜你喜欢

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

Win11怎么关闭开机自启动软件

Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)

Importance of EDA tools to chip industry knowledge popularization

深度学习 神经网络基础

基于图的 Affinity Propagation 聚类计算公式详解和代码示例

关联线探究,如何连接流程图的两个节点
![[Mysql]安装Mysql5.7](/img/c4/d7fb5ddf8e7be31f7a9ad68409e584.png)
[Mysql]安装Mysql5.7

Use Zadig to build a continuous delivery platform from 0 to 1

目标检测——Yolo系列
随机推荐
Write blog documents
升级版手机检测微信工具小程序源码-支持多种流量主模式
关于一个神奇函数的用法
EDA工具对芯片产业的重要性知识科普
How to create a pyramid with openmesh
新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码
网上开户是安全的吗?新手可以开炒股账户吗。
寫博客文檔
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
Iframe 父子页面通信
Arduino stepper library drive 28byj-48 stepper motor test program
Develop those things: easycvr cluster device management page function display optimization
Getting started with fastdfs
Détection des cibles - série Yolo
如果浏览器被意外关闭,react怎么缓存用户填写的表单?
On the usage of a magic function
Summary of SQL aggregate query method for yyds dry goods inventory
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
柒微自动发卡系统源码
PHP获取微信小程序和小程序商店外链地址