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)
相关推荐
All for pursuit.35 分钟前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
ZhangJun9538 分钟前
Mobike 共享单车分析项目
人工智能·python·算法·kmeans·聚类·knn
XLYcmy40 分钟前
PDF 论文处理器 — 优势与功能文档
大数据·python·网络安全·pdf·dify·rag·漏洞检测
Navigator_Z1 小时前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode
小猴子爱上树1 小时前
跨马翻译:批量图片翻译+视频字幕+智能抠图,跨境电商在线图片翻译工具
大数据·人工智能·python·音视频
winfredzhang1 小时前
用 Python 手写一个“所见即所得“的图片水印工具:wxPython + Pillow 实战详解
python·pillow·rotate·crop·身份改水印
xUxIAOrUIII1 小时前
日常笔记-1005-1
linux·笔记·python·macos
yl45301 小时前
内蒙热门的废酸再生处理企业
大数据·python
IT方大同2 小时前
java输入输出+方法
java·开发语言·python
Java后端的Ai之路2 小时前
Python 进阶探索30 - Python中的装饰器
开发语言·人工智能·python·文件处理·装饰器模式