【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 ...
随机推荐
- Linux内核调试方法总结之调试宏
本文介绍的内核调试宏属于静态调试方法,通过调试宏主动触发oops从而打印出函数调用栈信息. 1) BUG_ON 查看bug处堆栈内容,主动制造oops Linux中BUG_ON,WARN_ON用于调试 ...
- CentOS5.5配置Oracle监听 netca
在使用netca 配置监听时总是出现这个错误,即使更改了端口也会报错,,,也是在各种百度下, 找到了一个行之有效的办法: 在root下 step 1:netstat -a | grep 1521 确定 ...
- 【ABAP系列】SAP 销售订单的行项目里条件的增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 销售订单的行项目里条件的 ...
- 解决sql语句中参数为空(null)不会更新参数的问题
用的mybatis自动生成的 情景: 修改页面中,修改某个字段,修改前有数据,修改后为空. mybatis中一般用到 如:(这种直接忽略为空的字段,不能更新空字段参数) <update id=& ...
- python每日一练:0001题
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? import o ...
- (4.30)全面了解触发器:DML、DDL、LOGON触发器
DML.DDL.LOGON触发器 转自:https://www.cnblogs.com/seusoftware/p/9120632.html 触发器可以理解为由特定事件触发的存储过程, 和存储过程.函 ...
- TensorFlow 解决“ImportError: Could not find 'cudnn64_6.dll'”
解决“ImportError: Could not find 'cudnn64_6.dll'” 1. 问题描述 运行一个基于Tensorflow的代码时报错,如下所示: ImportError: Co ...
- HDU-1754 I Hate It(线段树,区间最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师 ...
- Python 数据分析中金融数据的来源库和简单操作
目录 金融数据 pandas-datareader TuShare 金融学图表 案例 金融数据 数据分析离不开数据的获取,这里介绍几种常用的获取金融方面数据的方法. pandas-datareader ...
- 类的基本概念及self是什么东西?
class 类 对一类拥有相同属性的对象的抽象.蓝图.原型,在类中定义了这些对象都具备的属性.共同方法 object对象 一个对象就是一个类的实例化模型,一个类必须经过实例化后才能在程序中调用,一个类 ...