题目如下:

Return the lexicographically smallest subsequence of text that contains all the distinct characters of text 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. 1 <= text.length <= 1000
  2. 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的更多相关文章

  1. LeetCode 1081. Smallest Subsequence of Distinct Characters

    原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  4. [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  6. 【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 ...

  7. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

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

  8. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

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

  9. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

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

随机推荐

  1. 高通Camera bring up软件流程【转】

    本文转载自:http://blog.csdn.net/liwei16611/article/details/51279658 高通camera bring up分为两种类型:YUV和bayerbrin ...

  2. mysql_Qcahce

    .cpu mem disk 如果是固态硬盘ssd那就是高速公路 火箭 高铁 普通公路 mysql 配置文件:windows 下 mysql.ini linux:my.cnf lamp路径:/opt/l ...

  3. MyBatis逆向工程去除表名前缀

    https://www.jianshu.com/p/e09d2370b796 https://blog.csdn.net/baidu_16757561/article/details/75071476 ...

  4. VMware 虚拟化编程(9) — VMware 虚拟机的快照

    目录 目录 前文列表 VMware 虚拟机的快照 快照的执行过程 删除快照 快照类型 Quiseced Snapshot 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDis ...

  5. memset, fill 对bool,int 赋值的效率

    memset对bool型变量赋false比对int型变量赋0快了10倍 fill对bool型变量赋false和对int型变量赋0效率一样 fill对int型变量赋0比memset对int型变量赋0慢了 ...

  6. 应用安全-Web安全-子域名/相关域名

    技巧 DNS解析记录 主站获取 单点登录接口 crossdomain.xml IP反查 通过HTTPS证书收集 DNS域传送搜集 联系人信息/邮箱反查域名 x-dns-prefetch-control ...

  7. 关于NewJson dll 引用不一致

    {System.IO.FileLoadException: 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKe ...

  8. Echats

    网址:https://www.echartsjs.com 1.特性 ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ...

  9. JavaWeb servlet,乱码的原因和解决

    请求为什么会有乱码? 答:当表单提交时,浏览器对中文参数值进行编码(使用打开表单所在的页面时的字符集进行编码,web服务器在默认情况下会使用iso-8859-1去解码,编码和解码方式不一致,就会产生乱 ...

  10. 防止 iframe 的链接重定向父级页面

    项目中发现,多系统通过iframe嵌套时,如果iframe的请求是重定向会导致父级页面重定向,怎么破? 查找MDN(https://developer.mozilla.org/zh-CN/docs), ...