Skip to content

Latest commit

 

History

History
20 lines (20 loc) · 485 Bytes

20有效的括号.md

File metadata and controls

20 lines (20 loc) · 485 Bytes
func isValid(s string) bool {
    stack := []byte{}
    brackets := map[byte]byte{
        '(': ')',
        '[': ']',
        '{': '}',
    }
    for i := 0; i < len(s); i++ {
        if s[i] == '(' || s[i] == '[' || s[i] == '{' {
            stack = append(stack, brackets[s[i]])
        } else if len(stack) > 0 && stack[len(stack)-1] == s[i] {
            stack = stack[:len(stack)-1]
        } else {
            return false
        }
    }
    return len(stack) == 0
}