-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeString.py
More file actions
44 lines (39 loc) · 1.23 KB
/
Copy pathdecodeString.py
File metadata and controls
44 lines (39 loc) · 1.23 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 字符串解码
# https://leetcode-cn.com/leetbook/read/queue-stack/gdwjv/
class Solution:
def decodeString(self, s: str) -> str:
print(s)
stack = []
buffer = ''
for each in s:
if each == '[':
if buffer != '':
stack.append(buffer)
buffer = ''
stack.append('[')
elif each == ']':
if buffer != '':
stack.append(buffer)
buffer = ''
temp = ''
while stack[-1] != '[':
temp = stack.pop() + temp
stack.pop()
stack.append(int(stack.pop()) * temp)
else:
if each.isdigit() and buffer.isdigit():
buffer += each
elif each.isdigit() and not buffer.isdigit():
if buffer != '':
stack.append(buffer)
buffer = ''
buffer += each
else:
buffer += each
if buffer != '':
stack.append(buffer)
result = ''.join(stack)
return result
case = "3[a2[c]]"
s = Solution().decodeString(case)
print(s)