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)
相关推荐
2301_804215417 分钟前
使用Python进行量化交易入门
jvm·数据库·python
全栈凯哥18 分钟前
27.Python datetime 与 time 完全指南
python
_日拱一卒25 分钟前
LeetCode:三数之和
算法·leetcode·排序算法
qiumingxun26 分钟前
Redis——使用 python 操作 redis 之从 hmse 迁移到 hset
数据库·redis·python
2401_8735449240 分钟前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
m0_5698814743 分钟前
进阶技巧与底层原理
jvm·数据库·python
Highcharts.js1 小时前
Highcharts for Python|用 Pythonic 的方式构建AI数据可视化图表
前端·人工智能·python·信息可视化·数据科学·highcharts·ai可视化
m0_726965981 小时前
关于conda
开发语言·python·conda
xxjj998a1 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
大尚来也1 小时前
Java 线程池深度解析:ThreadPoolExecutor 七大参数与核心原理
java·python·算法