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)
相关推荐
zx_741484817 分钟前
【Python入门】爬虫实战:Requests + XPath 从基础到实战
开发语言·爬虫·python
高洁019 分钟前
Teacher Forcing技术解析
人工智能·python·深度学习·transformer·知识图谱
2601_9620652517 分钟前
从零创建一个 Django 项目
后端·python·django
溪语流沙35 分钟前
【Python项目实战】虚拟环境与依赖管理:venv / pip / requirements.txt实操
开发语言·python·pip
梯度下降者1 小时前
CukeTest 自动化测试工具2023年度回顾白皮书
自动化测试·python·cuketest·qtquick/qml·linuxatk
ZC跨境爬虫1 小时前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
circuitsosk2 小时前
从零搭建行业知识平台:向量库+图数据库+传统关系库的多模检索统一层设计
数据库·python·oracle·向量数据库·rag·多模检索
whcyhhh2 小时前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
Navigator_Z2 小时前
LeetCode //C - 1220. Count Vowels Permutation
c语言·算法·leetcode
AR-26710-2 小时前
机器学习复习Day8——异常检测
人工智能·python·机器学习·scikit-learn