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)
相关推荐
im_AMBER25 分钟前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
JJ1M81 小时前
用 Python 快速搭建一个支持 HTTPS、CORS 和断点续传的文件服务器
服务器·python·https
leoufung1 小时前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
汤姆yu1 小时前
基于python大数据的小说数据可视化及预测系统
大数据·python·信息可视化
x***J3481 小时前
Python多线程爬虫
开发语言·爬虫·python
m***D2861 小时前
Python网络爬虫实战案例
开发语言·爬虫·python
ID_180079054732 小时前
基于 Python 的淘宝商品详情数据结构化解析:SKU、价格与库存字段提取
开发语言·数据结构·python
Laughtin2 小时前
终端Python环境的选择与切换
开发语言·python
JHC0000003 小时前
Python PDF 相关操作
开发语言·python·pdf
databook3 小时前
Manim进阶:用背景图片让你的数学视频脱颖而出
python·动效