class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack();
for(int i=0;i<s.length();i++){
char t=s.charAt(i);
if(t=='('||t=='{'||t=='['){
stack.push(t);
continue;
}
if(stack.isEmpty()){
return false;
}
char top = stack.pop();
if(top=='('&&t==')'){
continue;
}
if(top=='{'&&t=='}'){
continue;
}
if(top=='['&&t==']'){
continue;
}
return false;
}
if(stack.isEmpty()){
return true;
}
return false;
}
}
leetcode:4.有效的括号 简单
最新推荐文章于 2024-11-14 19:49:43 发布