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)
相关推荐
旖旎夜光12 分钟前
LeetCode 397:整数替换(贪心问题) —— 题解
数据结构·c++·算法·leetcode·贪心算法
事缓则圆17 分钟前
从零推导 Python 装饰器:不用背语法,看完这篇就懂了
python
鬼手点金19 分钟前
Hermes‑Agent 使用教程
python·ubuntu·powershell·cmd·ai agent·opencode·hermes
Gu Gu Study22 分钟前
【agent认知】宏观Agent的基本两层架构(Agent Loop与Agent Harness)
python·架构
从小就看凹凸曼^o^25 分钟前
Python基础3 - 流程控制语句:(1)程序结构
python
小陈的进阶之路1 小时前
爬虫三大解析库:lxml, jsonpath, bs4
开发语言·笔记·python
满怀冰雪1 小时前
17-正则化方法:Dropout、权重衰减与过拟合控制
人工智能·python·深度学习·机器学习·paddlepaddle
2603_965148111 小时前
抖音/快手直播选品:用API快速匹配爆款与低价货源
开发语言·python·自动化·api
用户7088769039381 小时前
RAG检索优化实战:从67%到92%,我做了这4步
python
GEOshijie1231 小时前
智能家居品牌做GEO推荐哪家供应商?品类词抢占案例复盘
人工智能·python·智能家居