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)
相关推荐
CodeBlog-star16 小时前
LLM安全实战:提示词注入与越狱攻击防御指南
python·agent·提示词攻击·越狱攻击
小柯南敲键盘16 小时前
电商图片翻译工具推荐,批量处理主图视频字幕,免费试用
大数据·人工智能·python·音视频
Nil20816 小时前
leetcode 160相交链表
算法·leetcode·链表
比高创意品牌策划设计17 小时前
零售卖场设计软件用CAD还是SketchUp还是酷家乐
python
迷迭香yy18 小时前
行业板块轮动因子实战从板块资金到因子建模的本地化Python全流程
数据库·人工智能·python
W_3260018 小时前
Python文件进阶:一维数据与 CSV 文件读写
开发语言·python
Tisfy19 小时前
LeetCode 3090.每个字符最多出现两次的最长子字符串:二重循环 / 滑动窗口
算法·leetcode·字符串·题解·模拟·双指针·滑动窗口
yaoxin52112319 小时前
497. Java 反射 - 使用反射读取注解
java·开发语言·python
2019一路前行19 小时前
Python 函数式编程
开发语言·python
专注仿真20 小时前
问答大模型技术方案算法实现-RAPTOR树构建算法与BEG集成使用
python·算法