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)
相关推荐
梨子串桃子_4 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
夏鹏今天学习了吗4 小时前
【LeetCode热题100(83/100)】最长递增子序列
算法·leetcode·职场和发展
文言一心5 小时前
LINUX离线升级 Python 至 3.11.9 操作手册
linux·运维·python
诗词在线5 小时前
中国古代诗词名句按主题分类有哪些?(爱国 / 思乡 / 送别)
人工智能·python·分类·数据挖掘
高锰酸钾_6 小时前
机器学习-L1正则化和L2正则化解决过拟合问题
人工智能·python·机器学习
AlenTech6 小时前
155. 最小栈 - 力扣(LeetCode)
算法·leetcode·职场和发展
天天睡大觉6 小时前
Python学习11
网络·python·学习
智航GIS6 小时前
11.11 Pandas性能革命:向量化操作与内存优化实战指南
python·pandas
写代码的【黑咖啡】7 小时前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化