当前位置:网站首页>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;
}
};
边栏推荐
- 【暑期每日一题】洛谷 P2637 第一次,第二次,成交!
- Pytorch(四) —— 可视化工具 Visdom
- The index is invalid
- How to do the performance pressure test of "Health Code"
- Kodori tree board
- Neural networks - use sequential to build neural networks
- Quelques outils dont les chiens scientifiques pourraient avoir besoin
- Basic usage, principle and details of session
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- LeetCode_ 28 (implement strstr())
猜你喜欢
Data loading and preprocessing
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
Section 27 remote access virtual private network workflow and experimental demonstration
Openresty rewrites the location of 302
Basic skeleton of neural network nn Use of moudle
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
C read / write application configuration file app exe. Config and display it on the interface
Pytest automated testing - compare robotframework framework
解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
Pytoch (IV) -- visual tool visdom
随机推荐
Research on medical knowledge atlas question answering system (I)
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
Common methods in transforms
Section 27 remote access virtual private network workflow and experimental demonstration
缓冲流与转换流
JVM栈和堆简介
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
Shell之一键自动部署Redis任意版本
分布式锁的实现
解决qiankun中子应用外链文件无法获取
分布式全局唯一ID解决方案详解
LeetCode_53(最大子数组和)
无器械健身
C#读写应用程序配置文件App.exe.config,并在界面上显示
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
【暑期每日一题】洛谷 P5886 Hello, 2020!
AssertionError assert I.ndim == 4 and I.shape[1] == 3
【FTP】FTP常用命令,持续更新中……
洗个冷水澡吧
Dede collection plug-in does not need to write rules