Decode String
394. Decode String
Input: s = "3[a]2[bc]"
Output: "aaabcbc"def decodeString(self, s: str) -> str:
stack = []
line = ''
num = 0
i = 0
while i < len(s):
c = s[i]
if c.isdigit():
num = int(c)
while i+1 < len(s) and s[i+1].isdigit():
num = 10*num + int(s[i+1])
i += 1
elif c == '[':
stack.append(line)
stack.append(num)
line = ''
num = 0
elif c == ']':
pre_num = stack.pop()
pre_line = stack.pop()
pre_line += line * pre_num
line = pre_line
else:
line += c
i += 1
return line443. Decode Compression
Valid Express (Bloomberg)
Compress String (Quantcast)
Iterative(較難)
Last updated