forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode-string.go
More file actions
executable file
·59 lines (49 loc) · 1016 Bytes
/
decode-string.go
File metadata and controls
executable file
·59 lines (49 loc) · 1016 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package problem0394
import (
"strconv"
)
func decodeString(s string) string {
n := len(s)
// i 是第一个数字的位置
i := 0
for i < n && (s[i] < '0' || '9' < s[i]) {
i++
}
if i == n {
// 没有数字,直接返回 s
return s
}
// j 是第一个 '[' 的位置
j := i + 1
// 由题意可知,s 很规范
// 存在数字的话,必定存在 '[' 和 ']'
for s[j] != '[' {
j++
}
// k 是与 j 的 '[' 对应的 ']' 的位置
k := j
count := 1
for count > 0 {
k++
if s[k] == '[' {
count++
} else if s[k] == ']' {
count--
}
}
// i:第一个数字的位置
// | j:第一个 '[' 的位置
// | | k:与 j 的 '[' 对应的 ']' 的位置
// ↓ ↓ ↓
// "abcd234[*******]efg"
// 题目说了, s 很规范
num, _ := strconv.Atoi(s[i:j])
return s[:i] + times(num, decodeString(s[j+1:k])) + decodeString(s[k+1:])
}
func times(n int, s string) string {
res := ""
for i := 0; i < n; i++ {
res += s
}
return res
}