【leetcode】316. Remove Duplicate Letters
题目如下:
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的更多相关文章
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【lintcode】834. Remove Duplicate Letters
题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- leetcode 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
随机推荐
- notepad++ 安装 hex_editor 十六进制查看插件
1.到 https://github.com/chcg/NPP_HexEdit/releases 去下载相应的文件,注意32位和64位的区别 2.点击notepad++的设置--->导入--&g ...
- The Rotation Game
题目链接 题意:有八种操作棋盘进行移动,使得中间8个数字一样,问·最短移动步数及如何移动. 思路:dfs,因为当中间八个数字中有m个数字不同时,至少需要m次操作,将这个m作为估值.
- css 图片 和 文本 的处理
图片 1.css3已经可以实现 img标签 和 img内图片分开处理的功能了.类似标签的背景图. https://www.zhangxinxu.com/wordpress/2015/03/css3 ...
- 【系统架构理论】一篇文章精通:Spring Cloud Netflix Eureka
是官方文档的总结 http://spring.io/projects/spring-cloud-netflix#overview 讲解基于2.0.2版本官方文档 https://cloud.sprin ...
- sql:CallableStatement执行存储过程
/** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...
- Skyline(6.x)-Web二次开发-1多窗口对比
一个页面加载多个 TerraExplorer3DWindow 和 SGWorld 等只有第一个能用(即使用 iframe 也是一样) 所以我决定打开两个新页面实现多窗口对比,然后我在<主页面&g ...
- 初学Selenium遇上的问题
1.IWebDriver driver = new InternetExplorerDriver();运行时报关于protecte model的错误 解决办法就是用如下代码设置IEDriverOpit ...
- PHP递归获得树形菜单和遍历文件夹下的所有文件以及子文件夹
PHP递归获得树形菜单和遍历文件夹下的所有文件以及子文件夹 一.使用递归获取树形菜单 数据表category(id,name,parent_id) <?php class category{ / ...
- centOS发布.Net Core 2.0 API
1.dotnet xxx.dll & & 放在启动参数后面表示设置此进程为后台进程.(目前测试无效) 2.ps -ef | grep xxx ps:将某个进程显示出来 -A 显示所有 ...
- event代表事件的状态,专门负责对事件的处理,它的属性和方法能帮助我们完成很多和用户交互的操作;
IE的event和其他的标准DOM的Event是不一样的,不同的浏览器事件的冒泡机制也是有区别 IE:window.event.cancelBubble = true;//停止冒泡window.eve ...