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.20 分钟前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode
y = xⁿ36 分钟前
关于Agent工程落地
前端·人工智能·python
longlongzihan39 分钟前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口
小小张说故事1 小时前
pip 安装慢、超时、报错?一份国内源配置 + 9 个高频报错对照表
python
在世修行1 小时前
干货:长任务反馈
python·长任务
东方芷兰1 小时前
Agent 技术摘要 05 —— BP、SaaS、B2B、C2B、B2C
java·笔记·python·语言模型·自然语言处理
老歌老听老掉牙1 小时前
麻花钻切削过程的力学与效率解析
python·钻头·切削
happylifetree1 小时前
Python27:核心语法-流程控制语句-match模式匹配
python
奋进的LY2 小时前
spacy的安装和使用教程
人工智能·python·深度学习·github
znx9392 小时前
机器学习赋能量化交易:解锁新时代投资的核心竞争优势
人工智能·python·机器学习·期魔方