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)
相关推荐
机器瓦力几秒前
Trae使用:重构一个项目
python·ai编程
jarreyer29 分钟前
python离线包安装方法总结
开发语言·python
码银1 小时前
【python】基于 生活方式与健康数据预测数据集(Lifestyle and Health Risk Prediction)的可视化练习,附数据集源文件。
开发语言·python·生活
墨染点香1 小时前
LeetCode 刷题【128. 最长连续序列】
算法·leetcode·职场和发展
熬了夜的程序员1 小时前
【LeetCode】82. 删除排序链表中的重复元素 II
数据结构·算法·leetcode·链表·职场和发展·矩阵·深度优先
星期天要睡觉2 小时前
大模型(Large Language Model, LLM)——什么是大模型,大模型的基本原理、架构、流程
人工智能·python·ai·语言模型
Q_Q19632884752 小时前
python+uniapp基于微信美食点餐系统小程序
spring boot·python·微信·django·flask·uni-app·node.js
KIKIiiiiiiii2 小时前
微信个人号开发中如何高效实现API二次开发
java·前端·python·微信
山顶听风3 小时前
Flask应用改用Waitress运行
前端·笔记·python·flask
熬了夜的程序员3 小时前
【LeetCode】83. 删除排序链表中的重复元素
算法·leetcode·链表