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)
相关推荐
Python私教7 分钟前
Django + AI 做智能工单系统:自动分类、重复合并、服务时限与人工审批实战
人工智能·python·django
qq_22589174669 分钟前
2026 计算机专业毕业设计选题推荐|Python 数据分析 / 可视化 / 推荐 / 预测(持续更新)
python·数据分析·课程设计
cui_ruicheng19 分钟前
Python数据分析(十一):数据可视化与 Matplotlib
python·信息可视化·数据分析·matplotlib
superrick25 分钟前
MCP 协议深度解析:AI Agent 的工具集成标准
python
淼澄研学1 小时前
Python自动化办公实操:基于pandas与requests的5个提效场景解析
python·自动化·pandas
薛少杰1 小时前
Python异步系统学习路线第59讲_核心原理与实战案例详解【技巧】
python·事件循环·异步编程·实战案例·协程调度
一次旅行2 小时前
fzf+ripgrep+fd终端三合一实战:一套检索工具链,大幅提升大型项目开发效率
人工智能·python·github
卷无止境2 小时前
当代码要上线前,谁在替你把关?——Python安全审查的方法论与实践
后端·python
卷无止境2 小时前
Python Dataclasses:让类定义回归简洁的艺术
后端·python