题目:
题解:
class Solution:
def decodeString(self, s: str) -> str:
stk = []
for ss in s:
# 不是"]",照单全收,进栈
if ss!="]":
stk.append(ss)
else:
# 遇到"]",把"[]"裹起来的单词取出,记为word
word = ""
while stk[-1]!="[":
word = stk.pop() + word
# 弹出"["
stk.pop()
# 因为会有大于10的次数,所以再写一个while,得到次数,如100
cnt = 0
t = 1
while len(stk) > 0 and ord("0") <= ord(stk[-1]) <=ord("9"):
cnt += int(stk.pop()) * t
t *= 10
# 得到重复的字符串后,继续压入栈
_s = cnt * word
stk.extend(list(_s))
#for _ss in _s:
# stk.append(_ss)
return "".join(stk)