LeetCode20 有效的括号

时间:2021-07-01 16:41:29   收藏:0   阅读:0

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

        dic = {‘{‘: ‘}‘,  ‘[‘: ‘]‘, ‘(‘: ‘)‘}
        stack = []
        for c in s:
            if c in dic: stack.append(c)
            elif not stack or dic[stack.pop()] != c: return False 
        return not stack

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!