当前位置:网站首页>LeetCode316-去除重复字母-栈-贪心-字符串
LeetCode316-去除重复字母-栈-贪心-字符串
2022-07-01 04:48:00 【李烦烦搞快点】
Note:
维护一个答案字符串,遍历给的字符串,每次判断一下当前这个字符,是不是比答案字符串的末尾小,如果小的话,那就看看这个末尾的能不能删,能删就删掉,不能删那没办法,就继续放着
代码如下:
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;
}
};
边栏推荐
- LeetCode_ 53 (maximum subarray and)
- 常用的Transforms中的方法
- VIM简易使用教程
- LeetCode_ 28 (implement strstr())
- LeetCode_35(搜索插入位置)
- [2020 overview] overview of link prediction based on knowledge map embedding
- Pytoch (III) -- function optimization
- RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
- C#读写应用程序配置文件App.exe.config,并在界面上显示
- 打印流与System.setout();
猜你喜欢

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

无器械健身

LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target

Pytoch (I) -- basic grammar

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

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

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

【暑期每日一题】洛谷 P5886 Hello, 2020!

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
![[hard ten treasures] - 1 [basic knowledge] classification of power supply](/img/a8/f129c9d15ca6ed99db1dacfc750ead.png)
[hard ten treasures] - 1 [basic knowledge] classification of power supply
随机推荐
线程安全问题
Matters behind the construction of paint testing laboratory
STM32扩展板 温度传感器和温湿度传感器的使用
pytorch中常用数据集的使用方法
【FTP】FTP连接时出现“227 Entering Passive Mode”的解决方法
分布式事务-解决方案
【暑期每日一題】洛穀 P1568 賽跑
Summary of acl2021 information extraction related papers
线程类的几大创建方法
最长递增子序列及最优解、动物总重量问题
pytorch神经网络搭建 模板
C -- array
LeetCode_58(最后一个单词的长度)
Common interview questions ①
Pytoch (IV) -- visual tool visdom
STM32 extended key scan
Neural networks - use sequential to build neural networks
1076 Forwards on Weibo
VIM简易使用教程
Query long transaction
