当前位置:网站首页>Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
2022-07-02 00:26:00 【Taotao can't learn English】
1047. Delete all adjacent duplicates in the string
Give a string of lowercase letters S, Duplicate deletion selects two adjacent and identical letters , And delete them .
stay S Repeat the delete operation on , Until you can't delete .
Returns the final string... After all the duplicates have been deleted . The answer is guaranteed to be unique .
Example :
- Input :“abbaca”
- Output :“ca”
- explain : for example , stay “abbaca” in , We can delete “bb” Because two letters are adjacent and the same , This is the only duplicate that can be deleted at this time . And then we get the string “aaca”, There is only “aa” You can perform a duplicate deletion operation , So the last string is “ca”.
Tips :
- 1 <= S.length <= 20000
- S It's only made up of lowercase letters .
Put the elements on the stack in reverse order , When entering, judge whether it is the same as the top element , The same is not true , Instead, it comes out of the stack . Finally, traverse the stack to get the result . Inverse is positive , This is the desired result . It feels easier to do double pointer . I'll try .
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. Delete all adjacent duplicates in the string **/
public class RemoveDuplicates {
/** * This problem can be solved by double pointer method , Stacks can do it, too * * @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--) {
// Directly stack when there is no element
if (stack.size() == 0) {
stack.push(chars[i]);
} else if (stack.peek() != chars[i]) {
// If the current element is different from the top element , Push
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"));
}
}

My attempt to write double pointer failed
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);

Similar to the sum of three numbers , The left side is relatively fixed , Slide right .
边栏推荐
- [cmake] cmake configuration in QT Creator
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- Vue force cleaning browser cache
- MySQL: the difference between insert ignore, insert and replace
- Shell custom function
- 【CTF】bjdctf_2020_babystack2
- 起床困难综合症(按位贪心)
- 回顾数据脱敏系统
- Selectively inhibiting learning bias for active sampling
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

JS——图片转base码 、base转File对象
![[QT] test whether QT can connect to the database](/img/63/32530c15995ef23bde8cadc3adfd11.png)
[QT] test whether QT can connect to the database

Pytorch learning record

LeetCode中等题题分享(5)

数据分析方法论与前人经验总结【笔记干货】

2023款雷克萨斯ES产品公布,这回进步很有感

GCC compilation
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017

Multi table operation - one to one, one to many and many to many
随机推荐
13 MySQL constraint
[cmake] cmake configuration in QT Creator
Learn online case practice
The origin of usb-if Association and various interfaces
Correlation - intra group correlation coefficient
Which app is better and more secure for stock mobile account opening
Windows installation WSL (II)
PWN attack and defense world cgpwn2
UVM tutorial
Multi table operation - one to one, one to many and many to many
牛客-练习赛101-推理小丑
mysql之B tree 以及 B+tree
4. Object mapping Mapstercover
股票开户哪个证券公司比较安全
2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Review data desensitization system
Use es to realize epidemic map or take out order function (including code and data)
时间复杂度与空间复杂度
PHP reads ini or env type configuration