当前位置:网站首页>LeetCode_ Monotonic stack_ Medium_ 316. Remove duplicate letters
LeetCode_ Monotonic stack_ Medium_ 316. Remove duplicate letters
2022-07-23 06:45:00 【Old street of small town】
1. subject
Give you a string s , Please remove the duplicate letters from the string , Make each letter appear only once . Ensure that the dictionary order of the returned results is the smallest ( The relative position of other characters should not be disturbed ).
Example 1:
Input :s = “bcabc”
Output :“abc”
Example 2:
Input :s = “cbacdcbc”
Output :“acdb”
Tips :
1 <= s.length <= 104
s It's made up of lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/remove-duplicate-letters
2. Ideas
(1) Monotonic stack
3. Code implementation (Java)
// Ideas 1———— Monotonic stack
class Solution {
public String removeDuplicateLetters(String s) {
int length = s.length();
Stack<Character> stack = new Stack<>();
/* cnt[0] Record string s In the character a Number of occurrences of cnt[1] Record string s In the character b Number of occurrences of ...... And so on */
int[] cnt = new int[26];
for (int i = 0; i < length; i++) {
cnt[s.charAt(i) - 'a']++;
}
// boolean The initial value of the elements in the array defaults to false
boolean[] inStack = new boolean[26];
for (char c : s.toCharArray()) {
// Every time you traverse a character , Reduce the corresponding times by one
cnt[c - 'a']--;
if (inStack[c - 'a']) {
continue;
}
while (!stack.isEmpty() && stack.peek() > c) {
// If there is no stack top element after , Then stop pop
if (cnt[stack.peek() - 'a'] == 0) {
break;
}
// If there are elements after , Then you can pop
inStack[stack.pop() - 'a'] = false;
}
stack.push(c);
inStack[c - 'a'] = true;
}
StringBuilder builder = new StringBuilder();
while (!stack.isEmpty()) {
builder.append(stack.pop());
}
return builder.reverse().toString();
}
}
边栏推荐
- Rllib学习 - [4] - AlgorithmConfig详细介绍 [Pytorch版]
- Add t.test significance to R language box diagram-
- 图文并茂演示小程序movable-view的可移动范围
- China's open source is moving towards the second tier!
- How to open win11 task manager? Skills and methods of opening win11 Task Manager
- 英飞凌推出全球首款采用后量子加密技术进行固件更新的TPM安全芯片
- 我用Flutter Deskstop做了一个Mars Xlog日志解析工具
- JS 复杂数据类型
- node进程对象的属性
- Understand JS prototype and prototype chain in one article
猜你喜欢

Learning Pyramid-Context Encoder Network for High-Quality Image Inpainting论文笔记

JS Boolean Undefined Null typeof 类型转换 number() parseInt() parseFloat String toString() 计算器

马尔科夫随机场:定义、性质,最大后验概率问题,能量最小化问题

Understand JS prototype and prototype chain in one article

力扣每日一题-第42天-171. Excel表列序号

Introduction and basic use of urllib basic use

【机器学习】模型选择(性能度量)原理及实战

I used fluent deskstop to build a Mars xlog log parsing tool

7.20 Codeforces Round #763 (Div. 2) C(二分) D(数学期望)背包+树形dp复习

微信小程序开发:第一个helloWorld
随机推荐
IDEA DEBUG启动一直卡着不动解决办法
Wechat applet development: the first HelloWorld
R语言绘制哑铃图
R语言 动态气泡图
最新可用的二维码生成 api
Derivative in R language
【JS】ES6-箭头函数
LSA related content in OSPF
1. Summary of strengthening learning foundation
Dao smart contract DAPP system development technology
selenium报错解决
Context Encoders: Feature Learning by Inpainting 论文笔记
Linear regression and logical regression and their implementation
LeetCode_单调栈_中等_316.去除重复字母
Feign remote call lost request header problem solution
Website disable F12 prohibit debugging code method
Introduction and basic use of urllib basic use
urllib的一个类型和六个方法
Learning Pyramid-Context Encoder Network for HighQuality Image Inpainting 论文笔记
Summer vacation notes 1