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)
相关推荐
Sw1zzle39 分钟前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode
海石1 小时前
子树怎么找?树的3种遍历方式来帮忙!
算法·leetcode
海石1 小时前
难度分 1588:思路 + 技巧 = AC
算法·leetcode
nothing&nowhere2 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田3 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang3 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
飞猪~7 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_956319887 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python
开飞机的舒克_9 小时前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi