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)
相关推荐
dlraba80216 分钟前
用 Python+OpenCV 实现实时文档扫描:从摄像头捕捉到透视矫正全流程
开发语言·python·opencv
小熊出擊22 分钟前
【pytest】fixture 内省(Introspection)测试上下文
python·单元测试·pytest
Haooog32 分钟前
98.验证二叉搜索树(二叉树算法题)
java·数据结构·算法·leetcode·二叉树
njsgcs41 分钟前
sse mcp flask 开放mcp服务到内网
后端·python·flask·sse·mcp
一人の梅雨1 小时前
1688 店铺商品全量采集与智能分析:从接口调用到供应链数据挖掘
开发语言·python·php
Terio_my1 小时前
Python制作12306查票工具:从零构建铁路购票信息查询系统
开发语言·python·microsoft
万粉变现经纪人2 小时前
如何解决 pip install -r requirements.txt 约束文件 constraints.txt 仅允许固定版本(未锁定报错)问题
开发语言·python·r语言·django·beautifulsoup·pandas·pip
站大爷IP2 小时前
Python定时任务实战:APScheduler从入门到精通
python
Fairy_sevenseven2 小时前
[1]python爬虫入门,爬取豆瓣电影top250实践
开发语言·爬虫·python
ThisIsMirror2 小时前
CompletableFuture并行任务超时处理模板
java·windows·python