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)
相关推荐
Mr_Xuhhh8 小时前
Java泛型进阶:从基础到高级特性完全指南
开发语言·windows·python
老天文学家了9 小时前
蓝桥杯备战Python
开发语言·python
ID_180079054739 小时前
除了 Python,还有哪些语言可以解析 JSON 数据?
开发语言·python·json
FreakStudio10 小时前
小作坊 GitHub 协作闭环:fork-sync-dev-pr-merge 实战指南
python·单片机·嵌入式·面向对象·电子diy
普通网友11 小时前
阿里云国际版服务器,真的是学生党的性价比之选吗?
后端·python·阿里云·flask·云计算
小陈工12 小时前
2026年4月2日技术资讯洞察:数据库融合革命、端侧AI突破与脑机接口产业化
开发语言·前端·数据库·人工智能·python·安全
陈晓明start12 小时前
【python】豆包模型,自动生成测试用例初探索
python
阿kun要赚马内12 小时前
Python中元组和列表差异:底层结构分析
开发语言·python
万添裁12 小时前
pytorch的张量数据结构以及各种操作函数的底层原理
人工智能·pytorch·python