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)
相关推荐
pilgrim538 分钟前
结合 Leetcode 题探究KMP算法
算法·leetcode
咚咚王者8 分钟前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
深蓝海拓42 分钟前
使matplot显示支持中文和负号
开发语言·python
AntBlack1 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e1 小时前
搭建本地deepseek大模型
python
1***Q7841 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
二进制的Liao2 小时前
【编程】脚本编写入门:从零到一的自动化之旅
数据库·python·算法·自动化·bash
Dxy12393102162 小时前
Python为什么要使用可迭代对象
开发语言·python
Keep_Trying_Go3 小时前
论文STEERER人群计数,车辆计数以及农作物计数算法详解(pytorch)
人工智能·pytorch·python
gzu_013 小时前
基于昇腾 配置pytorch环境
人工智能·pytorch·python