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)
相关推荐
databook18 分钟前
格式塔原理:数据可视化如何引导观众的注意力
python·数据分析·数据可视化
秦时明月之君临天下41 分钟前
Python递归获取目录大小
python
三万棵雪松1 小时前
【AI小智后端部分(一)】
人工智能·python·ai小智
laplace01231 小时前
Part 3:模型调用、记忆管理与工具调用流程(LangChain 1.0)笔记(Markdown)
开发语言·人工智能·笔记·python·langchain·prompt
winfredzhang1 小时前
深度解析:利用 Python + Playwright 攻克动态网页 PPT 导出难题
python·powerpoint·截图·自动翻页
only-qi1 小时前
leetcode2. 两数相加
算法·leetcode
风送雨1 小时前
八周Python强化计划(七)
开发语言·python
ππ很开心6661 小时前
DAY 32 函数专题2:装饰器
开发语言·python
山沐与山1 小时前
LangChain Tools解析:让Agent拥有超能力
人工智能·python·langchain