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)
相关推荐
宋均浩16 分钟前
AI 生成代码质量防线实战:5 个 CI/CD 配置,把 8% 的逻辑错拦在线上之前0
python
树码小子26 分钟前
Pycharm解释器配置问题解决
ide·python·pycharm
国雪34 分钟前
python-协程
python
一木 之林1 小时前
Python.六.(一)--1.标准库、常用工具与基础操作(进阶)
python·rpc·dubbo
月光船幽幽1 小时前
铁牌协议卡:节流与重构
python·重构
骇客野人2 小时前
Linux 服务器 Python 版 MCP 服务部署整体方案(适配 Claude/Cursor/自研Agent)
linux·服务器·python
AI视觉网奇2 小时前
动作识别 视频理解大模型
开发语言·python·音视频
Java尧哥学AI2 小时前
35岁Java程序员学AI Day2:从装环境到写第一行Python,踩了3个坑
python
zhiSiBuYu05172 小时前
Flask Session 与 Cookie 新手实战指南
后端·python·flask
码云骑士2 小时前
104-实战论文搜索引擎-ArXiv爬取-Milvus存储-RAG问答-Gradio前端
前端·python·搜索引擎·milvus