题目如下:

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

Example 1:

Input: "bcabc"
Output: "abc"

Example 2:

Input: "cbacdcbc"
Output: "acdb"

解题思路:因为要求字典序最小,所以先从'a'开始,找出第一个出现的'a',如果在这个'a'后面其余的字符至少会出现一次,则表示满足条件;如果'a'不满足这个条件,则继续判断'b',直到找出第一个满足条件并且字典序最小的字符。找到这个字典序最小的字符(假设为i)后,那么表示这个'i'可以保留,'i'之前的所有字符以及'i'之后其他的'i'都删除掉,在剩下的字符串中又继续从'a'开始,找到满足条件的除i以为最小的字典序字符。一直循环,直到所有不同字符都找到一个满足这个条件的即可。

代码如下:

class Solution(object):
def removeDuplicateLetters(self, s):
"""
:type s: str
:rtype: str
"""
dic = {}
for i,v in enumerate(s):
dic[v] = dic.setdefault(v,[]) + [i]
res = ''
origin_len = len(dic) while len(res) < origin_len:
#print dic
for (char) in range(ord('a'),ord('a')+26):
char = chr(char)
if char not in dic:
continue
flag = True
for key in dic.iterkeys():
if char == key:
continue
elif dic[char][0] > dic[key][-1]:
flag = False
break
if flag:
res += char
inx = dic[char][0]
del dic[char] import bisect
for key in dic.iterkeys():
del_inx = bisect.bisect_left(dic[key],inx)
dic[key] = dic[key][del_inx:]
break
return res

【leetcode】316. Remove Duplicate Letters的更多相关文章

  1. 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【lintcode】834. Remove Duplicate Letters

    题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  3. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  4. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  7. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  8. [LeetCode] 316. Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  9. leetcode 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

随机推荐

  1. 一张图理解"Figure", "Axes", "Axis"

    Figure is the object with the highest level in the hierarchy. It corresponds to the entire graphical ...

  2. leetcode-167周赛-1291-顺次数

    题目描述: 自己的提交: class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l,h = ...

  3. wangeditor 粘贴word内容带样式

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...

  4. 企业资源计划(ERP)

    ERP(企业资源计划)一般指企业资源计划(ERP) 物资资源管理(物流).人力资源管理(人流).财务资源管理(财流).信息资源管理(信息流) 信息技术在企业管理学上的应用可分做如下发展阶段:A. MI ...

  5. SpringBoot 快速构建微服务体系 知识点总结

    可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...

  6. (66) c# async await

    1.使用 async await 2.返回值 static void Main(string[] args) { Program p = new Program(); Console.WriteLin ...

  7. 用 Flask 来写个轻博客 (31) — 使用 Flask-Admin 实现 FileSystem 管理

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 编写 FileSystem Admin 页面 Flask-A ...

  8. linux shell assemble PDF文件

      daniel@daniel-mint ~/latex/linux/itext/daniel $ cat asm.sh header_start=0 header_len=15 xref_start ...

  9. mybatis分页插件使用

    一:导入依赖 <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> < ...

  10. [题解]Crazy Binary String-前缀和(2019牛客多校第三场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/B 题意: 给你一段长度为n,且只有 ‘0’ 和 ‘1’ 组成的字符串 a[0,...,n-1].求子串中 ‘ ...