发表时间:2023-03-18 阅读: 评论:
ans:
12345678910111213141516171819202122232425262728293031323334
class Solution { // 栈解决 利用先进后出的原理 及取反比较 public boolean isValid(String s) { if("".equals(s) || s.length() == 0){ return false; } int length = s.length(); Stack<Character> stack = new Stack(); for(char c : s.toCharArray()){ if(c == '('){ stack.push(')'); }else if(c=='{') stack.push('}'); else if(c=='[') stack.push(']'); else if (stack.empty()||c!=stack.pop()){ return false; } } if(stack.empty()){ return true; } return false; }}
__END__