【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下:
Return the lexicographically smallest subsequence of
text
that contains all the distinct characters oftext
exactly once.Example 1:
Input: "cdadabcc"
Output: "adbc"Example 2:
Input: "abcd"
Output: "abcd"Example 3:
Input: "ecbacba"
Output: "eacb"Example 4:
Input: "leetcode"
Output: "letcod"Note:
1 <= text.length <= 1000
text
consists of lowercase English letters.
解题思路:首先找出每个字母在text中的最大下标值并存入inx列表,接下来把text中的出现的每个字母的存入list并排好序:如果list[0]的字母在text中小标的最小值小于或者inx中所有元素的最小值,则表示该字母可以放在结果的第一个位置;如果不行则继续检查list[1]的字母,直到找出符合条件的字母放在第一位,然后在inx和list中删掉这个字母所对应的记录。之后继续循环查找,直到所有字母都找到为止。
代码如下:
class Solution(object):
def smallestSubsequence(self, text):
"""
:type text: str
:rtype: str
"""
dic = {}
for i in range(len(text)-1,-1,-1):
if text[i] not in dic:
dic[text[i]] = i tl = sorted(dic.iterkeys())
res = ''
start = 0
while len(tl) > 0:
for i in range(len(tl)):
inx = text.find(tl[i],start)
if inx <= min(dic.itervalues()):
res += tl[i]
start = inx + 1
del dic[tl[i]]
del tl[i]
break
return res
【leetcode】1081. Smallest Subsequence of Distinct Characters的更多相关文章
- LeetCode 1081. Smallest Subsequence of Distinct Characters
原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 【leetcode】1125. Smallest Sufficient Team
题目如下: In a project, you have a list of required skills req_skills, and a list of people. The i-th p ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- PHP必备函数详解
PHP必备函数详解
- tab下图表展示宽高为0的问题
tab下,默认展示第一个tab(最新订阅),第二个tab是echarts,需要动态获取父级div的宽高并赋值到图表的DOM的宽高.在实际开发过程中,发现无论如何延迟处理,或者mounted,第二个ta ...
- .bash_profile vs .bashrc
w http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
- __main__ — Top-level script environment
w 29.4. __main__ — Top-level script environment — Python 3.6.1 documentation https://docs.python.or ...
- (转)IDataGridViewEditingControl 接口 作用
本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814575 IDataGridViewEditingControl 接口 定义承载 ...
- django中间件(获取请求ip)
def simple_middleware(get_response): # 此处编写的代码仅在Django第一次配置和初始化的时候执行一次. print('1----django启动了') def ...
- cmd 编码修改。 牛阿。 解决问题
http://jingyan.baidu.com/article/e75aca85440f01142edac636.html 命令窗口修改编码,CMD编码修改方法 听语音 | 浏览:9696 | 更新 ...
- uni-app-小程序中组件不支持id选择器等
这个问题刚开始遇到的时候有点搞笑啰,正常情况下,id选择器不是最正常的吗?搞锤子哦. 接着我就找度娘了 ,官网给出的解释是:https://developers.weixin.qq.com/mini ...
- SpringBoot整合SpringMVC完成文件上传
1.编写Controller /** * SPringBoot文件上传 */ //@Controller @RestController //表示该类下的方法的返回值会自动做json格式的转换 pub ...
- python工程的结构
1 python系统库的位置 大部分系统库在/usr/lib64/python2.7目录下,但是像sys模块,是python内置的库,是用c实现的,直接连接进了python.exe中了. 也就是说,在 ...