mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
19 lines
354 B
Go
19 lines
354 B
Go
package leetcode
|
|
|
|
func minAddToMakeValid(S string) int {
|
|
if len(S) == 0 {
|
|
return 0
|
|
}
|
|
stack := make([]rune, 0)
|
|
for _, v := range S {
|
|
if v == '(' {
|
|
stack = append(stack, v)
|
|
} else if (v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(' {
|
|
stack = stack[:len(stack)-1]
|
|
} else {
|
|
stack = append(stack, v)
|
|
}
|
|
}
|
|
return len(stack)
|
|
}
|