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

类似三数之和的写法,左边相对固定,右边向右滑动。
边栏推荐
- Iframe 父子页面通信
- Learn white box test case design from simple to deep
- 关于一个神奇函数的用法
- On the next generation entrance of the metauniverse -- the implementation of brain computer interface
- ORA-01950
- Arduino stepper library drive 28byj-48 stepper motor test program
- A quietly rising domestic software, low-key and powerful!
- Entering Ruxin Town, digital intelligence transformation connects "future community"
- math_利用微分算近似值
- RichView 文档中的 ITEM
猜你喜欢

Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza

cocoaPods 添加成功后,导入不了头文件或者not found file 报错

8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide

How to turn off the boot auto start software in win11

Data analysts sound tall? Understand these points before you decide whether to transform

A quietly rising domestic software, low-key and powerful!

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

RichView 文档中的 ITEM

《軟件工程導論(第六版)》 張海藩 複習筆記

2022安全员-A证考题及在线模拟考试
随机推荐
ORA-01950
如果浏览器被意外关闭,react怎么缓存用户填写的表单?
Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
深度学习 神经网络基础
极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
在技术升级中迎合消费者需求,安吉尔净水器“价值战”的竞争之道
Principle of motion capture system
How to prevent repeated submission of new orders
Understand the structure in C language in one article
王者战力查询改名工具箱小程序源码-带流量主激励广告
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
目標檢測——Yolo系列
cocoaPods 添加成功后,导入不了头文件或者not found file 报错
《软件工程导论(第六版)》 张海藩 复习笔记
math_ Use differentiation to calculate approximate value
fastDFS入门
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
Use of common built-in classes of JS
Internship: gradually moving towards project development
Using qeventloop to realize synchronous waiting for the return of slot function