题目:

题解:
class Solution:
def isValid(self, s: str) -> bool:
if len(s) % 2 == 1:
return False
pairs = {
")": "(",
"]": "[",
"}": "{",
}
stack = list()
for ch in s:
if ch in pairs:
if not stack or stack[-1] != pairs[ch]:
return False
stack.pop()
else:
stack.append(ch)
return not stack
该篇文章介绍了Java中的Solution类方法isValid,用于检查输入字符串s中括号是否有效匹配。通过遍历字符串,利用栈数据结构判断配对字符是否正确。
518

被折叠的 条评论
为什么被折叠?



