LeetCode20 有效的括号
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 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
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 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