/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
#include <stdlib.h>
struct SS{
char A[10000];
int top;
};
bool isValid(char* s ) {
int k=0;
if(s[k]==NULL){
return true;
}
struct SS* B=(struct SS*)malloc(sizeof(struct SS)) ;
B->top=0;B->A[0]=NULL;
while(s[k] != NULL){
if(s[k] =='{'||s[k] =='['||s[k] =='('){
B->top++;
B->A[B->top]=s[k];
k++;
continue;
}
else if(s[k] =='}'||s[k] ==']'||s[k] ==')'){
if(s[k] =='}'&&B->A[B->top]=='{'){
k++;B->top--;
continue;
}
if(s[k] ==']'&&B->A[B->top]=='['){
k++;B->top--;
continue;
}
if(s[k] ==')'&&B->A[B->top]=='('){
k++;B->top--;
continue;
}
else return false;
}
}
if(B->top==0){
return true;
}
else return false;
// write code here
}
有效括号匹配问题。题目不同于括号匹配,题设简单,注意负溢出情况

京公网安备 11010502036488号