394. 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(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
res = ""
stNum = []
stChar = []
i = 0
while i < len(s):
if s[i].isdigit():
num = int(s[i])
while i+1 < len(s) and s[i+1].isdigit():
num = num * 10 + int(s[i+1])
i+=1
stNum.append(num)
elif s[i] == '[':
stChar.append(res)
res = ""
elif s[i] == ']':
tempNum = stNum.pop()
tempChar = stChar.pop()
while tempNum > 0:
tempChar += str(res)
tempNum -= 1
res = tempChar
else:
res += str(s[i])
i+=1
return res
Last updated