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)
相关推荐
geovindu5 小时前
python: Memento Pattern
开发语言·python·设计模式·备忘录模式
寻星探路5 小时前
【JVM 终极通关指南】万字长文从底层到实战全维度深度拆解 Java 虚拟机
java·开发语言·jvm·人工智能·python·算法·ai
lbb 小魔仙5 小时前
【Java】Java 实战项目:手把手教你写一个电商订单系统
android·java·python
岱宗夫up6 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
Dxy12393102166 小时前
中文乱码恢复方案
开发语言·python
rongyili886 小时前
Dify 外部知识库集成 Milvus 实战指南
开发语言·python·milvus
Hello eveybody7 小时前
什么是动态规划(DP)?(Python版)
python·动态规划
sprintzer7 小时前
2.06-2.15力扣数学刷题
算法·leetcode·职场和发展
南 阳7 小时前
Python从入门到精通day34
开发语言·python
滴滴答滴答答9 小时前
LeetCode Hot100 之 17 有效的括号
算法·leetcode·职场和发展