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)
相关推荐
tryCbest3 分钟前
Django 基础入门教程(第四篇):Form组件、Auth认证、Cookie/Session与中间件
python·django
王夏奇4 分钟前
Python-对excel文件操作的总览
开发语言·python·excel
2501_941982055 分钟前
Python 实现企业微信外部群机器人:轻量化消息推送方案
python·机器人·企业微信
骇客野人7 分钟前
python爬虫例子,且处理反爬的网站也能爬
开发语言·爬虫·python
hutengyi8 分钟前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
老天文学家了9 分钟前
蓝桥杯:直线
python
铁手飞鹰12 分钟前
eBUS SDK Python环境安装
开发语言·python
放下华子我只抽RuiKe514 分钟前
智聊机器人进阶:从 API 调试到全功能交互界面的完美落地
开发语言·人工智能·python·机器学习·分类·机器人·交互
放下华子我只抽RuiKe516 分钟前
构建企业级私有化 AI:从大模型原理到本地智聊机器人全栈部署指南
开发语言·人工智能·python·深度学习·机器学习·分类·机器人
ID_1800790547318 分钟前
python采集小红书笔记详情API接口,json数据返回
笔记·python·json