思路:入栈,入栈时如果空栈那么直接入栈,否则检查栈顶和下一个字符是否匹配,匹配则出栈,否则继续入栈。
AC代码:
import java.util.*;
public class Parenthesis {
public boolean chkParenthesis(String A, int n) {
// write code here
Stack<Character> stack = new Stack<Character>();
return judge(A,stack,n);
}
public boolean judge(String string,Stack<Character> stack,int n){
for(int i = 0;i<n;i++){
if(i==0){
stack.push(string.charAt(i));
continue;
}
else if(!stack.isEmpty()&&stack.peek()=='('&&string.charAt(i)==')'){
stack.pop();
}
else{
stack.push(string.charAt(i));
}
}
if(stack.isEmpty()){
return true;
}else{
return false;
}
}
}