public boolean isValid(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s == null || "".equals(s)){
return true;
}
Stack<Integer> stack = new Stack<Integer>();
int length = s.length();
for(int i = 0; i < length; i++){
char c = s.charAt(i);
if(c == '(' || c == '[' || c == '{'){
stack.push(c - '(');
}else if(c == ')' || c == ']' || c== '}'){
if(stack.size() <= 0)
return false;
Integer j = stack.pop();
if(c == ')'){
if(j.intValue() != '(' -'('){
return false;
}
}else if(c == ']'){
if(j.intValue() != '[' -'('){
return false;
}
}else if(c == '}'){
if(j.intValue() != '{' -'('){
return false;
}
}
}else{
return false;
}
}
if(stack.size() > 0){
return false;
}
return true;
}
Valid Parentheses
最新推荐文章于 2021-02-23 15:41:56 发布