Python | Leetcode Python题解之第316题去除重复字母

avatar
作者
筋斗云
阅读量:0

题目:

题解:

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)

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!