public static boolean isValid(String s) {
int len = s.length();
char[] stack = new char[s.length()];
int top = -1;
int i = 0;
for(i = 0; i < len; i++) {
char c = s.charAt(i);
if(c == '(' || c == '{' || c == '[') {
stack[++top] = c;
}else{
if((c == ')' && top !=-1 && stack[top] == '(') || (c == ']' &&top !=-1 && stack[top] == '[') || (c == '}' && top !=-1 && stack[top] == '{')) {
top--;
}else{
break;
}
}
}
return top == -1 && i >= len ? true: false;
}
20. Valid Parentheses
最新推荐文章于 2024-07-09 23:43:07 发布