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)
相关推荐
Want5958 分钟前
Python炫酷烟花①
开发语言·python
青少儿编程课堂18 分钟前
蓝桥杯+全国青少年信息素养大赛+电子学会PythonC/C++考级:最长公共子序列LCS解析
c++·python·算法·bfs·信息学竞赛
Y38153266227 分钟前
印度电商数据采集教程:用 SerpBase 查 Flipkart/Myntra/Ajio 商品(附代码)
数据库·python
阿维的博客日记1 小时前
大白话解析快排Python
python
圣保罗的大教堂1 小时前
leetcode 3727. 最大交替平方和 中等
leetcode
doujiaozh1 小时前
我在 DOS 6.22 上跑通了 Kimi AI:一场跨越 40 年的硬核接力
python
凛铄linshuo1 小时前
分析业务流程什么时候适合断言?
python·pytest
Xin7702 小时前
LeetCode 200. 岛屿数量(DFS 深度优先遍历法)
leetcode
import_random2 小时前
[python]IDE可以自动检测venv工具创建的虚拟环境.venv吗
python
楠风测开2 小时前
Python学习路线:搭好环境,跑通第一个脚本
python