当前位置:网站首页>Leetcode316- remove duplicate letters - stack - greedy - string
Leetcode316- remove duplicate letters - stack - greedy - string
2022-07-01 04:53:00 【Li Fan, hurry up】
Note:
Maintain an answer string , Traverse the given string , Judge the current character each time , Is it smaller than the end of the answer string , If it's small , Let's see if the last one can be deleted , Delete if you can , You can't delete it. There's no way , Just keep it
The code is as follows :
class Solution {
public:
string removeDuplicateLetters(string s) {
string stk;
unordered_map<char, bool> ins;
unordered_map<char, int> last;
for(int i = 0; i < s.size(); i ++) last[s[i]] = i;
for(int i = 0; i < s.size(); i ++){
if(ins[s[i]]) continue;
while(stk.size() && stk.back() > s[i] && last[stk.back()] > i){
ins[stk.back()] = false;
stk.pop_back();
}
ins[s[i]] = true;
stk += s[i];
}
return stk;
}
};
边栏推荐
- [daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
- 解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
- Pytoch (I) -- basic grammar
- FileInputStream
- Pytorch(二) —— 激活函数、损失函数及其梯度
- 分布式全局唯一ID解决方案详解
- 神经网络的基本骨架-nn.Moudle的使用
- Neural network - nonlinear activation
- 分布式事务-解决方案
- FileOutPutStream
猜你喜欢

Introduction to JVM stack and heap

Pytoch (IV) -- visual tool visdom

Use of STM32 expansion board temperature sensor and temperature humidity sensor
![[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply](/img/c2/6dfb9f477306edb46ff2a6a6ca32dd.png)
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply

最长递增子序列及最优解、动物总重量问题

The longest increasing subsequence and its optimal solution, total animal weight problem

解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key

Pytest automated testing - compare robotframework framework

STM32扩展板 数码管显示

Pytorch(二) —— 激活函数、损失函数及其梯度
随机推荐
Pytorch(一) —— 基本语法
[daily question in summer] letter delivery by p1629 postman in Luogu (to be continued...)
AssertionError assert I.ndim == 4 and I.shape[1] == 3
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
Distributed - summary list
[hard ten treasures] - 1 [basic knowledge] classification of power supply
[daily question in summer] Luogu p7222 [rc-04] informatics competition
Shell analysis server log command collection
1076 Forwards on Weibo
每日一题-LeetCode1175-质数排列-数学
【暑期每日一題】洛穀 P1568 賽跑
CF1638E. Colorful operations Kodori tree + differential tree array
Basic skeleton of neural network nn Use of moudle
解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
pytorch中常用数据集的使用方法
[summer daily question] Luogu p5886 Hello, 2020!
分布式数据库数据一致性的原理、与技术实现方案
FileOutPutStream
LeetCode_ 58 (length of last word)
Design experience of Meizhou clinical laboratory
