Decode String(状态机)
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".class Solution:
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
stack = []
ret=''
for i in s:
if i == ']':
temp = scnt= ''
while stack and stack[-1].isalpha():
temp = stack.pop() + temp
if stack[-1] == '[':
stack.pop()
while stack and stack[-1].isdigit():
scnt = stack.pop() + scnt
cnt = int(scnt)
temp *= cnt
if stack:
stack += temp//不空,重压入栈
else:
ret += temp //空了,返回结果
else: stack.append(i)
rem = ''.join(stack) if stack else ''//记得可能有残余单字母
return ret + remLast updated