mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
34 lines
564 B
Go
34 lines
564 B
Go
package leetcode
|
|
|
|
func scoreOfParentheses(S string) int {
|
|
res, stack, top, temp := 0, []int{}, -1, 0
|
|
for _, s := range S {
|
|
if s == '(' {
|
|
stack = append(stack, -1)
|
|
top++
|
|
} else {
|
|
temp = 0
|
|
for stack[top] != -1 {
|
|
temp += stack[top]
|
|
stack = stack[:len(stack)-1]
|
|
top--
|
|
}
|
|
stack = stack[:len(stack)-1]
|
|
top--
|
|
if temp == 0 {
|
|
stack = append(stack, 1)
|
|
top++
|
|
} else {
|
|
stack = append(stack, temp*2)
|
|
top++
|
|
}
|
|
}
|
|
}
|
|
for len(stack) != 0 {
|
|
res += stack[top]
|
|
stack = stack[:len(stack)-1]
|
|
top--
|
|
}
|
|
return res
|
|
}
|