给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号
bool isValid(char * s){
char a[10000]=" ",t;
int i=0,top=0;
while(s[i]!='\0'){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
if(s[i]=='(') t=')';
if(s[i]=='{') t='}';
if(s[i]=='[') t=']';
top++;
a[top]=t;
}
else{
if(a[top]!=s[i])
return false;
else
top--;
}
i++;
}
if(top==0)
return true;
else
return false;
}