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)
相关推荐
留白_1 分钟前
pandas练习题
python·数据分析·pandas
码界索隆9 分钟前
Python转Java系列:面向对象基础
java·开发语言·python
逻辑星辰13 分钟前
x-ds-pow-response逆向分析
开发语言·人工智能·python·深度学习·算法
c_lb728826 分钟前
涨跌停与流动性变差还要不要挂单:quote 涨跌停字段与熔断思路
python·区块链
非生而知之者30 分钟前
基于灰狼算法优化的电量预测
python·算法·群体智能算法·电力预测
AI行业学习38 分钟前
CC‑Switch v3.16.1 免费下载(Windows+macOS+Linux)、使用方法【2026.6.11】
linux·开发语言·windows·python·macos·前端框架·html
_Evan_Yao39 分钟前
递归函数入门:以阶乘和斐波那契数列为例
python·学习·算法
DFT计算杂谈1 小时前
WannierTools输入文件wt.in一键批量生成脚本
java·前端·chrome·python·算法·conda
weixin_468466851 小时前
网络数据采集新手入门指南
python·网络爬虫·conda·编程