-
Notifications
You must be signed in to change notification settings - Fork 0
/
brackets.go
39 lines (38 loc) · 907 Bytes
/
brackets.go
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
package vege
// RemoveInvalidParentheses 去除s中不成对的括号,pair为成对的括号
func RemoveInvalidParentheses(s string, pair [2]rune) string {
sr := []rune(s)
result := make([]rune, 0, len(sr))
//栈操作
stack := make([]int, 0)
removeRight := 0
for i, ru := range sr {
if ru == pair[0] {
stack = append(stack, i-removeRight)
} else if ru == pair[1] {
if len(stack) > 0 {
stack = stack[:len(stack)-1]
} else {
removeRight++
continue
}
}
result = append(result, ru)
}
//分割字符串
str := make([]rune, 0, len(result))
if len(stack) > 0 {
str = append(str, result[:stack[0]]...)
for i := 1; i < len(stack); i++ {
str = append(str, result[stack[i-1]+1:stack[i]]...)
}
//结尾内容
last := stack[len(stack)-1]
if last < len(result)-1 {
str = append(str, result[last+1:]...)
}
} else {
str = result
}
return string(str)
}