Python | Leetcode Python题解之第316题去除重复字母

题目:

题解:

python 复制代码
class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        vis = defaultdict(int)
        cnt = defaultdict(int)
        for ch in s: cnt[ch] += 1
        queue = []
        for ch in s:
            if vis[ch] == 0:
                while queue and queue[-1] > ch and cnt[queue[-1]]:
                    vis[queue.pop()] = 0
                if not queue or queue != ch: queue.append(ch)
                vis[ch] = 1
            cnt[ch] -= 1
        return "".join(queue)
相关推荐
努力学习的小廉4 分钟前
Python基础——搭建 Python 环境
开发语言·python
清水白石00815 分钟前
Python 编程全景解析:四大核心容器的性能较量、语义之美与高阶实战
开发语言·数据库·python
2401_8785302115 分钟前
深入理解Python的if __name__ == ‘__main__‘
jvm·数据库·python
liuyao_xianhui27 分钟前
优选算法_栈_删除字符中的所有相邻重复项_C++
开发语言·数据结构·c++·python·算法·leetcode·链表
STLearner32 分钟前
AI论文速读 | 元认知监控赋能深度搜索:认知神经科学启发的分层优化框架
大数据·论文阅读·人工智能·python·深度学习·学习·机器学习
老虎062735 分钟前
LeetCode热题100 刷题笔记(第三天)链表 「两数相加」
笔记·leetcode·链表
林姜泽樾39 分钟前
python入门第四课,运算符、转义字符和文本
python·pycharm
PieroPc1 小时前
用tkinter 做一个通过 扫描仪硬件 扫描纸质文档的软件 支持pdf
python·pdf·扫描
Z.风止1 小时前
Large Model-learning(1)
开发语言·笔记·git·python·学习
会编程的土豆1 小时前
【leetcode hot 100】二叉树
算法·leetcode