-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path394.py
28 lines (27 loc) · 856 Bytes
/
394.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
# coding=utf-8
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
p, retStr = 0, ''
stack = []
flag = 0
while p < len(s):
if s[p] != ']': stack.append(s[p]) # push element into stack
else:
tmp, num = '', ''
while len(stack) and stack[-1] != '[':
tmp += stack.pop()
stack.pop()
while len(stack) and ord(stack[-1])>=48 and ord(stack[-1])<=57:
num += stack.pop()
num = int(num[::-1])
for v in num*tmp[::-1]: # flat the repeated string into the stack
stack.append(v)
p += 1
for c in stack:
retStr += c
return retStr