1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
remove-duplicate-letters.cpp 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(n)
// Space: O(k), k is size of the alphabet
// vector solution, need to know size of the alphabet in advance (4ms)
class Solution {
public:
string removeDuplicateLetters(string s) {
const int k = 26;
vector<int> remaining(k);
for (const auto& c : s) {
++remaining[c - 'a'];
}
vector<bool> in_stack(k);
string stk;
for (const auto& c : s) {
if (!in_stack[c - 'a']) {
while (!stk.empty() && stk.back() > c && remaining[stk.back() - 'a']) {
in_stack[stk.back() - 'a'] = false;
stk.pop_back();
}
stk.push_back(c);
in_stack[c - 'a'] = true;
}
--remaining[c - 'a'];
}
return stk;
}
};
// Time: O(n)
// Space: O(k), k is size of the alphabet
// hash solution, no need to know size of the alphabet in advance (16ms)
class Solution2 {
public:
string removeDuplicateLetters(string s) {
unordered_map<char, int> remaining;
for (const auto& c : s) {
++remaining[c];
}
unordered_set<char> in_stack;
string stk;
for (const auto& c : s) {
if (!in_stack.count(c)) {
while (!stk.empty() && stk.back() > c && remaining[stk.back()]) {
in_stack.erase(stk.back());
stk.pop_back();
}
stk.push_back(c);
in_stack.emplace(c);
}
--remaining[c];
}
return stk;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助