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)
相关推荐
zhanghongyi_cpp10 分钟前
第4章 学习自测
python·学习
李松桃12 分钟前
Python爬虫基础
开发语言·爬虫·python·get请求使用方法·post请求使用方法
weixin_3077791313 分钟前
Python用正则把 SQL 关键字整词匹配后转成小写
开发语言·python·sql·算法
维爱智VR认知学能43 分钟前
VR认知训练自适应难度引擎:基于滑动窗口准确率与反应时的状态机实现
python·软件工程·架构设计
荣码1 小时前
多模态应用入门:让AI看懂图片再回答问题,我踩了4个坑
java·python
羲云网络2 小时前
GPT Image 2.6 标注引导编辑:透明 PNG、markup 请求与局部验收
图像处理·人工智能·python·api
富贵儿9982 小时前
NL2SQL 实践手记 09:答案正确以后,还能更快吗——一次可核对的 AI4DB 实验
数据库·python·sql·ai·自然语言处理·langchain
小赖同学啊2 小时前
多模态 一般组合方案
python
劲夫学编程2 小时前
leetcode: 字母异位词分组
算法·leetcode
AI创界者2 小时前
Python 进阶实战(第八十五章):基于 Redis Lua 原子租约的分布式锁、看门狗续期与本地事务表/Outbox 一致性实战
人工智能·python·aigc