-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394_decodeString.py
More file actions
32 lines (30 loc) · 914 Bytes
/
394_decodeString.py
File metadata and controls
32 lines (30 loc) · 914 Bytes
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
class Solution:
def decodeString(self, s: str) -> str:
# 用两个栈来取代函数栈的作用
strs = []
times = []
string = ""
size = len(s)
num = 0
for i in range(size):
c = s[i]
if(c.isdigit()):
a = int(c)
num = num * 10 + a
else:
if(num != 0):
times.append(num)
num = 0
if(c == "["):
strs.append(string)
string = ""
elif(c == "]"):
# 这个时候应该做出栈操作
out = strs.pop()
time = times.pop()
string = out + time * string
else:
string += c
return string
solution = Solution()
solution.decodeString("100[leetcode]")