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)
相关推荐
小陈phd14 小时前
langGraph从入门到精通(四)——基于LangGraph的State状态模式设计
python·microsoft·状态模式
38242782714 小时前
JS正则表达式实战:核心语法解析
开发语言·前端·javascript·python·html
Engineer邓祥浩14 小时前
设计模式学习(10) 23-8 装饰者模式
python·学习·设计模式
ybdesire14 小时前
Joern服务器启动后cpgqls-client结合python编程进行扫描
运维·服务器·python
autho14 小时前
conda
linux·python·conda
知乎的哥廷根数学学派15 小时前
基于注意力机制的多尺度脉冲神经网络旋转机械故障诊断(西储大学轴承数据,Pytorch)
人工智能·pytorch·python·深度学习·神经网络·机器学习
测试199815 小时前
用Postman测WebSocket接口
自动化测试·软件测试·python·websocket·测试工具·接口测试·postman
l1t15 小时前
数独优化求解C库tdoku-lib的使用
c语言·开发语言·python·算法·数独
有一个好名字15 小时前
力扣-奇偶链表
算法·leetcode·链表
wxm63115 小时前
力扣算法题(C++):1、2
java·算法·leetcode