Files
LeetCode-Go/leetcode/0856.Score-of-Parentheses/856. Score of Parentheses.go
2020-08-07 17:06:53 +08:00

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
}