【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下:
Return the lexicographically smallest subsequence of
textthat contains all the distinct characters oftextexactly 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 <= 1000textconsists 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 ...
随机推荐
- Tarjan算法整理
众所周知,tarjan是个非常nb的人,他发明了很多nb的算法,tarjan算法就是其中一个,它常用于求解强连通分量,割点和桥等.虽然具体实现的细节不太一样,但是大体思路是差不多的.先来说一下大体思路 ...
- 术语-EDI:EDI
ylbtech-术语-EDI:EDI 电子数据交换(Electronic data interchange,缩写EDI)是指按照同一规定的一套通用标准格式,将标准的经济信息,通过通信网络传输,在贸易伙 ...
- java对接短信平台
短信验证码目前是比较主流验证身份的一种方式,下面分享下我对接的几种短信平台 阿里云短信:https://api.alidayu.com/docs/api.htm?spm=a3142.7395905.4 ...
- 深入理解webpack基本配置(一)
1. 安装webpack到全局 在学习构建之前,我们来在本地文件新建一个存放项目的文件夹,比如叫demo1这个项目,然后进入demo1该项目的根目录后,执行命令 npm init运行下,一路回车(先简 ...
- AlertManager警报通知 E-mail 微信 模板
# AlertManager警报通知 E-mail 微信 模板 #AlertManager配置 #alertmanager.yml # 全局配置项 global: resolve_timeout: 5 ...
- 类File
* File类常用的构造方法: * (1)File(String s);//由s确定File对象的文件名 * (2)File(String directory,String s);//由directo ...
- winform项目中开发的一套UI控件库
https://github.com/houyhea/winform-control-lib winform-control-lib 曾经在一个winform项目中开发的一套UI控件库 类图: 效果 ...
- 16/7/7_PHP-方法重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- SpringBoot整合SpringMVC完成文件上传
1.编写Controller /** * SPringBoot文件上传 */ //@Controller @RestController //表示该类下的方法的返回值会自动做json格式的转换 pub ...
- 孩子王app自动签到送现金券
今天在某商场的孩子王店给小孩买东西时发现,app连续签到3天即可获得一张5元现金券. 回到家中,花几分钟Fiddler抓包分析写了个自动签到的代码. var cookies = new string[ ...