Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 628 Bytes

74. 有效的括号.md

File metadata and controls

22 lines (20 loc) · 628 Bytes

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。

class Solution:
    def isValid(self, s: str) -> bool:
        hashmap = {'(':')','[':']','{':'}'}
        stack = []
        
        for c in s:
            if c in hashmap:
                stack.append(c)
            else:
                if stack:
                    top = stack[-1]
                    if c != hashmap[top]:
                        return False
                    else:
                        stack.pop()
                else:
                    return False
        return not stack