首页后端开发Pythontext中按字典序排列最小的子序列

text中按字典序排列最小的子序列

时间2023-06-02 17:20:01发布访客分类Python浏览245
导读:text中按字典序排列最小的子序列?class Solution(object :def smallestSubsequence(self, text :""":type text: str:rtype: str"""stack = []l...

text中按字典序排列最小的子序列?

class Solution(object):

def smallestSubsequence(self, text):

"""

:type text: str

:rtype: str

"""

stack = []

last_o = { }

considered = { }

for i in range(len(text)-1,-1,-1):

if text[i] not in last_o:

last_o[text[i]] = i

considered[text[i]] = False

print(last_o)

i = 0

while i len(text):

print(stack,i,text[i])

if len(stack) == 0:

stack.append(text[i])

considered[text[i]] = True

i+=1

elif stack[-1]> text[i] and considered[text[i]] == False:

if last_o[stack[-1]]> i:

considered[stack[-1]]=False

stack.pop()

else:

considered[text[i]] = True

stack.append(text[i])

i+=1

elif stack[-1]text[i] and considered[text[i]] == False:

stack.append(text[i])

considered[text[i]] = True

i+=1

else:

i+=1

return "".join(i for i in stack)

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: text中按字典序排列最小的子序列
本文地址: https://pptw.com/jishu/58495.html
毕业脚本怎么写 先赋值在自增和先自增再赋值的区别

游客 回复需填写必要信息