class Solution {
public boolean isValid(String s) {
//初始化一个栈
Stack<Character> stack = new Stack<Character>();
//利用栈的先进后出的特性去检验括号能不能配对
for(char c : s.toCharArray()){
//碰到左括号入栈
if(c == '(') stack.push(')');
else if(c == '[') stack.push(']');
else if(c == '{') stack.push('}');
//碰到右括号就出栈,如果不等于之前入栈的括号就返回false,如果栈为空也返回false
//c != stack.pop() 如果不相等也会出栈,但是会但返回false
else if(stack.isEmpty() || c != stack.pop()) return false;
}
return stack.isEmpty();
}
}