LeetCode 394. 字符串解码
题目描述
思路分析
栈
参考代码
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
func decodeString(s string) string {
numStack := make([]int, 0)
strStack := make([]string, 0)
curStr, num := "", 0
for i := 0; i < len(s); i++ {
c := s[i]
if c >= '0' && c <= '9' {
num = num*10 + int(c-'0')
} else if c == '[' {
numStack = append(numStack, num)
strStack = append(strStack, curStr)
curStr, num = "", 0
} else if c == ']' {
count := numStack[len(numStack)-1]
numStack = numStack[:len(numStack)-1]
preStr := strStack[len(strStack)-1]
strStack = strStack[:len(strStack)-1]
curStr = preStr + strings.Repeat(curStr, count)
} else {
curStr += string(c)
}
}
return curStr
}
1
write your code here
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用