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)
相关推荐
wfbcg几秒前
每日算法练习:LeetCode 125. 验证回文串 ✅
算法·leetcode·职场和发展
We་ct1 分钟前
LeetCode 295. 数据流的中位数:双堆解法实战解析
开发语言·前端·数据结构·算法·leetcode·typescript·数据流
迷藏4945 分钟前
**雾计算中的边缘智能:基于Python的轻量级任务调度系统设计与实现**在物联网(IoT)飞速发展的今天,传统云
java·开发语言·python·物联网
biubiubiu070610 分钟前
从 Python 和 Node.js 的流行看 Java 的真实位置
java·python·node.js
大江东去浪淘尽千古风流人物15 分钟前
【Basalt】Basalt void SqrtKeypointVioEstimator<Scalar_>::optimize() VIO优化流程
数据库·人工智能·python·机器学习·oracle
CoberOJ_43 分钟前
(2026-04-01更新)小白自己写,量化回测系统stock-quant(六)
python·ai·股票·量化·交易·回测·a股港股美股
qq_283720051 小时前
Python:time/datetime 模块教程
python·时间·模块·日期
小辉同志1 小时前
739. 每日温度
c++·算法·leetcode
qq_252614411 小时前
PyMySQL 对 caching_sha2_password 支持不完善
python
Via_Neo1 小时前
二进制枚举
数据结构·算法·leetcode