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)
相关推荐
YuanDaima20481 分钟前
双指针基础原理与题目说明
数据结构·人工智能·python·算法·leetcode·手撕代码
hef2882 分钟前
怎么诊断MongoDB Config Server响应极慢的问题_高频Auto-split导致的元库写入压力
jvm·数据库·python
qq_380619167 分钟前
html怎么用deno运行_Deno如何作为本地服务器运行HTML文件
jvm·数据库·python
小鱼~~7 分钟前
进程和线程
python
断眉的派大星17 分钟前
pytorch中保存训练模型和加载训练模型的用法
人工智能·pytorch·python
C+++Python17 分钟前
如何学习Python的应用领域知识?
开发语言·python·学习
疯狂打码的少年20 分钟前
【Day12 Java转Python】Python工程的“骨架”——模块、包与__name__
java·开发语言·python
ueotek27 分钟前
Ansys Zemax | 在 MATLAB 或 Python 中使用 ZOS-API 进行光线追迹的批次处理
python·matlab·ansys·zemax·光学软件
u01074754637 分钟前
mysql如何实现高可用集群架构_基于MHA环境搭建与部署
jvm·数据库·python
qq_380619161 小时前
如何在phpMyAdmin中处理特殊字符账号名的授权_反引号的正确包裹
jvm·数据库·python