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)
相关推荐
勇往直前plus6 分钟前
Redis&Python 梳理
数据库·redis·python
开源量化GO7 分钟前
多品种组合单品种剧烈波动:组合风控先平谁
python
战族狼魂15 分钟前
AI 全栈开发实战训练路线(企业级)
人工智能·python·chatgpt·大模型
AC赳赳老秦16 分钟前
用 OpenClaw 制定技术学习计划:根据目标岗位自动生成学习路线、推荐学习资源
开发语言·c++·人工智能·python·mysql·php·openclaw
长和信泰光伏储能30 分钟前
探索绿色能源未来:光伏储能技术解析
python
李白的天不白41 分钟前
config/WebMvcConfig.java
开发语言·python
Elaine3361 小时前
基于Django框架的静态个人名片网站设计
后端·python·django·mvt
henrylin99991 小时前
开源量化软件功能介绍
python·开源
C+-C资深大佬1 小时前
Python 新手学习指南
开发语言·python
嫂子的姐夫2 小时前
050-wx小程序合肥住房
爬虫·python·小程序·逆向