题意:括号匹配
思路:栈的使用
代码:
public class ValidParentheses {
public boolean isValid(String string){
if(string == null || string.length() == 0) return false;
Stack<Character> stack = new Stack<>();
for(int i = 0 ; i < string.length() ; i++){
if(string.charAt(i) == '(' || string.charAt(i) == '{' || string.charAt(i) == '['){
stack.push(string.charAt(i));
}else{
if(stack.size() == 0) return false;
char temp = stack.pop();
if(string.charAt(i) == ')'){
if(temp != '(') return false;
}
if(string.charAt(i) == ']'){
if(temp != '[') return false;
}
if(string.charAt(i) == '}'){
if(temp != '{') return false;
}
}
}
return true;
}