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)
相关推荐
大神薯条老师1 小时前
Python高级爬虫之js逆向+安卓逆向1.3节:Python数据类型
爬虫·python·深度学习·机器学习·数据分析·网络爬虫
想跑步的小弱鸡1 小时前
Leetcode hot 100(last day)
算法·leetcode·哈希算法
仙人掌_lz5 小时前
利用python从零实现Byte Pair Encoding(BPE):NLP 中的“变形金刚”
开发语言·python·gpt·自然语言处理·llm·token·deepseek
羊小猪~~5 小时前
深度学习项目--分组卷积与ResNext网络实验探究(pytorch复现)
网络·人工智能·pytorch·python·深度学习·神经网络·机器学习
q567315235 小时前
使用Alamofire下载网站首页内容
开发语言·爬虫·python·scrapy·golang
龙俊杰的读书笔记6 小时前
[leetcode] 面试经典 150 题——篇9:二叉树(番外:二叉树的遍历方式)
数据结构·算法·leetcode·面试
Aerkui6 小时前
Python标准库-copy
开发语言·python
Try,多训练7 小时前
Pytorch查看神经网络结构和参数量
人工智能·pytorch·python
Swift社区8 小时前
从表格到序列:Swift 如何优雅地解 LeetCode 251 展开二维向量
开发语言·leetcode·swift
hweiyu008 小时前
Python从入门到精通全套视频教程免费
开发语言·python