当前位置:网站首页>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 .
边栏推荐
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
- 挖财学堂开户打新债安全可靠嘛?
- Timer和ScheduledThreadPoolExecutor的区别
- 实例讲解将Graph Explorer搬上JupyterLab
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
- cookie、session、tooken
- Material design component - use bottomsheet to show extended content (I)
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
猜你喜欢
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
USB-IF协会与各种接口的由来
2022拼多多详情/拼多多商品详情/拼多多sku详情
LDR6035智能蓝牙音响可对手机设备持续充放电方案
如何提升数据质量
Dongge cashes in and the boss retires?
Niuke - Practice 101 - reasoning clown
Asp . Text of automatic reply to NETCORE wechat subscription number
[opencv450] hog+svm and hog+cascade for pedestrian detection
Linux CentOS7安装Oracle11g的超完美新手教程
随机推荐
js 公共库 cdn 推荐
Jielizhi, production line assembly link [chapter]
[QT] solve the problem that QT MSVC 2017 cannot compile
Node - generate wechat permission verification configuration
mysql之B tree 以及 B+tree
Node -- egg creates a local file access interface
Resumption of attack and defense drill
Relevant settings of wechat applet cache expiration time (recommended)
Timer和ScheduledThreadPoolExecutor的区别
[QT] test whether QT can connect to the database
Node——添加压缩文件
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
JS -- image to base code, base to file object
JS——图片转base码 、base转File对象
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Jielizhi, production line assembly link [chapter]
heketi 记录
基于全志H3的QT5.12.9移植教程
Selectively inhibiting learning bias for active sampling
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]