当前位置:网站首页>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 .
边栏推荐
- Node -- egg creates a local file access interface
- Review data desensitization system
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- 回顾数据脱敏系统
- Is it safe and reliable to open an account in Caixue school and make new debts?
- 一个实习生的CnosDB之旅
- Heketi record
- SQL Server 安装指南
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- 在证券账户上买基金安全吗?哪里可以买基金
猜你喜欢

Example explanation: move graph explorer to jupyterlab

SQL Server Installation Guide

Asp . Text of automatic reply to NETCORE wechat subscription number

What is ThreadLocal memory leak and how to solve it

Intelligent operation and maintenance practice: banking business process and single transaction tracking

基于全志H3的QT5.12.9移植教程

【CMake】Qt creator 里面的 cmake 配置

回顾数据脱敏系统
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

Relatively easy to understand PID understanding
随机推荐
Node -- egg implements the interface of uploading files
The difference between timer and scheduledthreadpoolexecutor
[QT] QT cannot find a solution to the compiler using msvc2017
Flow control statement of SQL data analysis [if, case... When detailed]
SQL Server Installation Guide
使用多线程Callable查询oracle数据库
Jielizhi Bluetooth headset quality control and production skills [chapter]
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Database -- sqlserver details
Windows installation WSL (II)
Digital transformation has a long way to go, so how to take the key first step
[QT] solve the problem that QT MSVC 2017 cannot compile
From 20s to 500ms, I used these three methods
GCC compilation
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
mysql之B tree 以及 B+tree
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
Learn online case practice
[cascade classifier training parameters] training Haar cascades
【QT】对于Qt MSVC 2017无法编译的问题解决