IT评测·应用市场-qidao123.com

标题: Python | Leetcode Python题解之第316题去除重复字母 [打印本页]

作者: 一给    时间: 2024-8-5 04:32
标题: Python | Leetcode Python题解之第316题去除重复字母
题目:

题解:
  1. class Solution:
  2.     def removeDuplicateLetters(self, s: str) -> str:
  3.         vis = defaultdict(int)
  4.         cnt = defaultdict(int)
  5.         for ch in s: cnt[ch] += 1
  6.         queue = []
  7.         for ch in s:
  8.             if vis[ch] == 0:
  9.                 while queue and queue[-1] > ch and cnt[queue[-1]]:
  10.                     vis[queue.pop()] = 0
  11.                 if not queue or queue != ch: queue.append(ch)
  12.                 vis[ch] = 1
  13.             cnt[ch] -= 1
  14.         return "".join(queue)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4