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)
相关推荐
Niu_brave6 分钟前
Python基础知识学习(2)
开发语言·python·学习
geekrabbit13 分钟前
Ubuntu 22.04上安装Python 3.10.x
linux·python·ubuntu
deflag26 分钟前
第T1周:Tensorflow实现mnist手写数字识别
人工智能·python·机器学习·分类·tensorflow
Zucker n37 分钟前
猫狗识别大模型——基于python语言
开发语言·python
数云界1 小时前
使用 VSCode 在 Python 中创建项目环境
ide·vscode·python
m0_608570981 小时前
vscode对python进行多卡调试
ide·vscode·python
zhang-ge1 小时前
python提取pdf表格到excel:拆分、提取、合并
python·pdf·excel
计算机学姐1 小时前
基于python+django+vue的在线学习资源推送系统
开发语言·vue.js·python·学习·django·pip·web3.py
吃什么芹菜卷2 小时前
2024.9最新:CUDA安装,pytorch库安装
人工智能·pytorch·笔记·python·深度学习
鬼圣2 小时前
【软件测试】--xswitch将请求代理到测试桩
软件测试·python·flask·测试桩·xswitch插件