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)
相关推荐
summer_du8 分钟前
Qdrant
人工智能·python
logomister设计公司阿燕9 分钟前
华为商标“减法哲学”:极简主义如何成就全球品牌?
python·华为
萧青山11 分钟前
2026国标合规:Agent Loop合规设计模式的6种循环架构(GB/Z 185.6+Python)
python·设计模式·agent loop·gb/z 185·国标合规
zzzzzz31028 分钟前
我用 AI 编程半年,这 5 个 Prompt 技巧让我效率翻倍
python·机器学习·编程语言
To_OC8 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
Scott9999HH9 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
AI云海10 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰10 小时前
[爬虫]-Urllib
爬虫·python
玉鸯12 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent