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)
相关推荐
李可以量化几秒前
QMT 量化实践:两种方式获取个股上市日期(内置 Python + 原生 Python 完整可运行代码)
python
是多巴胺不是尼古丁9 分钟前
期末java复习--string
java·开发语言·python
garmin Chen25 分钟前
从 Transformer 到 Agent:大模型技术全景解析
java·人工智能·python·深度学习·transformer
没有钱的钱仔31 分钟前
pytorch_cuda安装
人工智能·pytorch·python
Full Stack Developme37 分钟前
Apache Tika 教程
java·开发语言·python·apache
Navigator_Z40 分钟前
LeetCode //C - 1096. Brace Expansion II
c语言·算法·leetcode
笨笨没好名字1 小时前
Leetcode刷题python版第一周
python·算法·leetcode
Cthy_hy1 小时前
斯特林数:组合划分的递归经典,一二两类全解
python·算法·斯特林数
青春:一叶知秋1 小时前
【Python】python基本语法和使用
开发语言·python
一只齐刘海的猫1 小时前
【Leetcode】无重复字符的最长子串
算法·leetcode·职场和发展