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)
相关推荐
iAkuya3 小时前
(leetcode)力扣100 14合并区间(差分/排序)
算法·leetcode·职场和发展
weixin_421585014 小时前
PYTHON 迭代器1 - PEP-255
开发语言·python
月明长歌4 小时前
【码道初阶】【LeetCode 958】判定完全二叉树:警惕 BFS 中的“管中窥豹”陷阱
算法·leetcode·宽度优先
hxxjxw4 小时前
Pytorch分布式训练/多卡训练(六) —— Expert Parallelism (MoE的特殊策略)
人工智能·pytorch·python
dagouaofei5 小时前
PPT AI生成实测报告:哪些工具值得长期使用?
人工智能·python·powerpoint
BoBoZz195 小时前
ExtractPolyLinesFromPolyData切割一个三维模型(球体),并可视化切割后产生的多条等高线
python·vtk·图形渲染·图形处理
quikai19815 小时前
python练习第六组
java·前端·python
Trouville015 小时前
Python中encode和decode的用法详解
开发语言·python
月明长歌6 小时前
【码道初阶】【LeetCode 102】二叉树层序遍历:如何利用队列实现“一层一层切蛋糕”?
java·数据结构·算法·leetcode·职场和发展·队列