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)
相关推荐
RechoYit5 分钟前
数学建模——评价与决策类模型
python·算法·数学建模·数据分析
查尔char13 分钟前
CentOS 7 编译安装 Python 3.10 并解决 SSL 问题
python·centos·ssl·pip·python3.11
独隅29 分钟前
Python `with` 语句 (上下文管理器) 深度解析与避坑指南
开发语言·python
做怪小疯子33 分钟前
Python 基础学习
开发语言·python·学习
Eward-an1 小时前
高效构建长度为 n 的开心字符串中第 k 小的字符串
python·leetcode
Bert.Cai1 小时前
Python time.sleep函数作用
开发语言·python
shughui1 小时前
Miniconda下载、安装、关联配置 PyCharm(2026最新图文教程)
ide·python·pycharm·miniconda
rgb2gray2 小时前
论文详解 | TWScan:基于收紧窗口的增强扫描统计,实现不规则形状空间热点精准检测
网络·人工智能·python·pandas·交通安全·出租车
小鸡吃米…2 小时前
Python线程同步
开发语言·数据结构·python
清水白石0082 小时前
Python 弱引用深度解析——让缓存不再成为内存泄漏的温床
java·python·缓存