遍历字符串每次遇到,(,{,【 存储到 栈 ,如果遇到 ),},】 则从栈顶哪一个元素,
判断是否匹配,如果不匹配返回false ;
最后返回栈顶 ==-1 这个是判断是否栈顶为空若为空那么字符串是空;
class Solution {
private int []stack = new int[10001];
private int top = -1;
public void push(int val){
stack[++top] = val;
}
public int pull(){
if(top==-1)
return -1;
return stack[top--];
}
public boolean isValid(String s) {
for(int i = 0;i<s.length();i++){
int tmp = s.charAt(i);
if(tmp=='('||tmp=='{'||tmp=='['){
push(tmp);
}else
{
int side = pull();
if(tmp==')'&&side!='('||tmp=='}'&&side!='{'||tmp==']'&&side!='[')
return false;
}
}
return top==-1;
}
}