-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.go
50 lines (47 loc) · 903 Bytes
/
test.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
40
41
42
43
44
45
46
47
48
49
50
package main
import "fmt"
func scoreOfParentheses(s string) int {
chars := []rune(s)
r := 0
for i := 0; i < len(chars)-1; i++ {
c, next := chars[i], chars[i+1]
if '(' == c && '(' == next {
sub := ""
depth := 1
for i = i + 1; i < len(chars); i++ {
if '(' == chars[i] {
depth++
} else {
depth--
}
if 0 == depth {
break
}
sub += string(chars[i])
}
if len(chars)-1 == i {
return 2 * scoreOfParentheses(sub)
}
return 2*scoreOfParentheses(sub) + scoreOfParentheses(s[i+1:])
} else if '(' == c && ')' == next {
sub := ""
depth := 1
for i = i + 2; i < len(chars); i++ {
if '(' == chars[i] {
depth++
} else {
depth--
}
if 0 == depth {
break
}
sub += string(chars[i])
}
return 1 + scoreOfParentheses(sub)
}
}
return r
}
func main() {
fmt.Println(scoreOfParentheses("(())()"))
}