当前位置:网站首页>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;
}
};
边栏推荐
- LeetCode316-去除重复字母-栈-贪心-字符串
- 【暑期每日一题】洛谷 P2026 求一次函数解析式
- The design points of voice dialogue system and the importance of multi round dialogue
- pytorch神经网络搭建 模板
- [daily question in summer] Luogu p2026 find the analytic formula of primary function
- The longest increasing subsequence and its optimal solution, total animal weight problem
- How to use common datasets in pytorch
- C -- array
- LeetCode_ 66 (plus one)
- How to do the performance pressure test of "Health Code"
猜你喜欢

Pytorch convolution operation

技术分享| 融合调度中的广播功能设计

Fitness without equipment
![[summer daily question] Luogu p5886 Hello, 2020!](/img/ac/4be05f80aab7fb766674e6e2d16fbc.png)
[summer daily question] Luogu p5886 Hello, 2020!

Distributed architecture system splitting principles, requirements and microservice splitting steps

Pytoch (I) -- basic grammar

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

Manually implement a simple stack

STM32扩展板 数码管显示

分布式-总结列表
随机推荐
解决qiankun中子应用外链文件无法获取
分布式数据库数据一致性的原理、与技术实现方案
STM32扩展版 按键扫描
神经网络的基本骨架-nn.Moudle的使用
Pico neo3 handle grabs objects
js解决浮点数相乘精度丢失问题
Implementation of distributed lock
How to do the performance pressure test of "Health Code"
Cmake selecting compilers and setting compiler options
LeetCode_ 53 (maximum subarray and)
FileOutPutStream
Neural network convolution layer
Pytorch(二) —— 激活函数、损失函数及其梯度
Pytoch (IV) -- visual tool visdom
Basic exercise of test questions hexadecimal to decimal
打印流与System.setout();
常用的Transforms中的方法
【暑期每日一题】洛谷 P2026 求一次函数解析式
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
分布式-总结列表
