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

类似三数之和的写法,左边相对固定,右边向右滑动。
边栏推荐
猜你喜欢

Détection des cibles - série Yolo

STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download

2022熔化焊接与热切割上岗证题目模拟考试平台操作

Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.

编译原理复习笔记

On the next generation entrance of the metauniverse -- the implementation of brain computer interface

漏洞复现-.Net-ueditor上传

Win11 how to hide the taskbar? Win11 method to hide the taskbar

联想电脑怎么连接蓝牙耳机?

math_ Use differentiation to calculate approximate value
随机推荐
运动捕捉系统原理
STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download
Use Zadig to build a continuous delivery platform from 0 to 1
Niuke programming question -- must brush the string of 101 (brush the question efficiently, draw inferences from one instance)
数据分析师听起来很高大上?了解这几点你再决定是否转型
What if the win11 shortcut key switching input method doesn't respond? Shortcut key switching input method does not respond
Is it safe to open an account online? Can a novice open a stock trading account.
薛定谔的日语学习小程序源码
1592 example 1 King (sgu223 loj10170 luogu1896 increase + / provincial election -) violent thinking pressure DP 01 Backpack
三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
如何用OpenMesh创建一个四棱锥
Oracle 死锁测试
人脸识别系统 —— OpenCV人脸检测
强大的万年历微信小程序源码-支持多做流量主模式
How to connect the two nodes of the flow chart
300题线性代数 第四讲 线性方程组
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
ORA-01950