leetcode——316. 去除重复字母

这道题也不是我自己想出来的。。。

class Solution(object):
    def removeDuplicateLetters(self, s):
        """
        :type s: str
        :rtype: str
        """
        counts = {}
        coun=set(s)
        for c in coun:
            counts[c]=s.count(c)

        stack, stacked = ['0'], set()   # stack为答案,放置哨兵,stacked为stack中已有的字符
        for c in s:
            if c not in stacked:
                while c < stack[-1] and counts[stack[-1]]:  # 当栈顶在后面还有且大于当前字符时弹出
                    stacked.remove(stack.pop())
                stack.append(c)
                stacked.add(c)
            counts[c] -= 1
        return ''.join(stack[1:])
执行用时 :32 ms, 在所有 python 提交中击败了62.20%的用户
内存消耗 :11.7 MB, 在所有 python 提交中击败了44.44%的用户
 
 
——2019.11.4
我的前方是万里征途,星辰大海!!
原文地址:https://www.cnblogs.com/taoyuxin/p/11793009.html