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)
相关推荐
CoderYanger21 分钟前
A.每日一题——2536. 子矩阵元素加 1
java·线性代数·算法·leetcode·矩阵
sanggou27 分钟前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
geng_zhaoying1 小时前
在VPython中使用向量计算3D物体移动
python·3d·vpython
半tour费2 小时前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友2 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
百锦再2 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
普通网友2 小时前
Python函数定义与调用:编写可重用代码的基石
jvm·数据库·python
倦王2 小时前
力扣日刷251117
算法·leetcode·职场和发展
普通网友2 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
MZ_ZXD0013 小时前
springboot流浪动物救助平台-计算机毕业设计源码08780
java·spring boot·后端·python·spring·flask·课程设计