当前位置:网站首页>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;
}
};
边栏推荐
- 1076 Forwards on Weibo
- Pytorch(二) —— 激活函数、损失函数及其梯度
- Technology sharing | broadcast function design in integrated dispatching
- [daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
- Summary of testing experience - Testing Theory
- RuntimeError: “max_pool2d“ not implemented for ‘Long‘
- 解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *
- STM32扩展板 温度传感器和温湿度传感器的使用
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- RDF query language SPARQL
猜你喜欢

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

Detailed explanation of distributed global unique ID solution

LeetCode522-最长特殊序列II-哈希表-字符串-双指针

STM32扩展版 按键扫描

One click shell to automatically deploy any version of redis

Difficulties in the development of knowledge map & the importance of building industry knowledge map
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

pytorch 卷积操作

C read / write application configuration file app exe. Config and display it on the interface

STM32 extended key scan
随机推荐
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
Distributed transactions - Solutions
LeetCode_35(搜索插入位置)
【暑期每日一题】洛谷 P7222 [RC-04] 信息学竞赛
Detailed explanation of distributed global unique ID solution
LeetCode_ 35 (search insertion position)
洗个冷水澡吧
C -- array
LeetCode522-最长特殊序列II-哈希表-字符串-双指针
打印流与System.setout();
[summer daily question] Luogu p5886 Hello, 2020!
[une question par jour pendant l'été] course luogu p1568
Talk about testdeploy
对象的序列化与反序列化
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*
Difference between cookie and session
Distributed - summary list
Use of STM32 expansion board temperature sensor and temperature humidity sensor
技术分享| 融合调度中的广播功能设计
Kodori tree board
