LeetCode "Remove Duplicate Letters" !!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode "Remove Duplicate Letters" !!相关的知识,希望对你有一定的参考价值。
Interesting Greedy.. classic
https://leetcode.com/discuss/75529/c-simple-solution-easy-understanding
class Solution { public: string removeDuplicateLetters(string s) { vector<int> cnt(26); vector<bool> visited(26, false); // Count for(char c : s) cnt[c - ‘a‘] ++; stack<char> st; for(char c : s) { int inx = c - ‘a‘; cnt[inx]--; if(visited[inx]) continue; // Greedy: we keep lexi-inc at every step. Greedy sort // we pop it as long previous choice is larger in ASCII and not the last. while (!st.empty() && c < st.top() && cnt[st.top() - ‘a‘] != 0) { visited[st.top() - ‘a‘] = false; st.pop(); } st.push(c); visited[inx] = true; } string ret; while(!st.empty()) { ret = st.top() + ret; st.pop(); } return ret; } };
以上是关于LeetCode "Remove Duplicate Letters" !!的主要内容,如果未能解决你的问题,请参考以下文章
javascript Andela删除iamuchejude创建的Javascript中的重复测试 - https://repl.it/@iamuchejude/Andelas-remove-dup
LeetCode--To Lower Case & Remove Outermost Parentheses (Easy)
[Leetcode] Remove Duplicates from Sorted Array II
LeetCode 301. Remove Invalid Parentheses(DP)
Axon MongoDB - message='E11000 重复键错误集合 uniqueAggregateIndex dup key: : "101", : 0