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)
相关推荐
code tsunami3 分钟前
【无标题】
开发语言·python
CodeGL23 分钟前
10、Python基础之进程和线程
开发语言·python
TechWayfarer1 小时前
PHP漏洞被利用,怎么揪出攻击IP?
开发语言·python·tcp/ip·网络安全·php
java_nnnn1 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
2601_967980112 小时前
Python列表操作方法详解
python·数据类型·修改·列表操作·功能汇总
Navigator_Z2 小时前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
Patrick在香港3 小时前
台风在变少,来香港的没有
python·数据分析·香港·开放数据·热带气旋
qq_406981033 小时前
大白话rust系列09 函数调用两次有什么错?
开发语言·python·rust
benchmark_cc3 小时前
QuantDash 安装成功后,如何验证 Python 环境可以正常导入 SDK?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
zh路西法4 小时前
【模型式强化学习】(二)PETS——从概率动力学模型到模型预测控制(MPC)
pytorch·python·强化学习·pe·mpc·ces·pets