class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') stack.push(c);
else {
if (!stack.empty() && ((c == ')' && stack.peek() == '(') || (c == ']' && stack.peek() == '[') || (c == '}' && stack.peek() == '{'))) {
stack.pop();
}
else return false;
}
}
return stack.empty();
}
}
有效的括号
最新推荐文章于 2022-03-14 19:15:00 发布