seo关于网站搜索排名关键词的标准评定,网站优化提升速度,百度不喜欢wordpress,wordpress自定义分类发目录 一、思路
二、源码 一、思路
左括号入栈#xff0c;遇到右括号则出栈进行匹配。
1、如果不匹配#xff0c;false
2、如果匹配完#xff0c;栈不空#xff0c;false
3、如果栈空#xff0c;但是还有右括号#xff0c;false
二、源码 // 支持动态增长的栈
typed…目录 一、思路
二、源码 一、思路
左括号入栈遇到右括号则出栈进行匹配。
1、如果不匹配false
2、如果匹配完栈不空false
3、如果栈空但是还有右括号false
二、源码 // 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top; // 栈顶int capacity; // 容量
}Stack;// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空如果为空返回非零结果如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
// 初始化栈
void StackInit(Stack* ps)
{STDataType* temp (STDataType*)malloc(sizeof(STDataType) * 4);if (temp NULL){perror(malloc fail \n);exit(-1);}ps-a temp;ps-capacity 4;ps-top -1;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{assert(ps);if (ps-capacity ps-top 1){STDataType* temp (STDataType*)realloc(ps-a, sizeof(STDataType) * ps-capacity * 2);if (temp NULL){perror(realloc fail \n);exit(-1);}ps-a temp;ps-capacity * 2;}ps-a[ps-top 1] data;ps-top;
}
// 出栈
void StackPop(Stack* ps)
{assert(ps);if (ps-top -1){return ;}ps-top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{assert(ps);if (ps-top -1){return -1;}return ps-a[ps-top];
}// 检测栈是否为空如果为空返回非零结果如果不为空返回0
int StackEmpty(Stack* ps)
{assert(ps);if (ps-top -1)return 1;elsereturn 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{free(ps-a);ps-a NULL;ps-capacity 0;ps-top 0;printf(destory success!\n);
}bool isValid(char* s) {Stack st;StackInit(st);while (*s){//左括号入栈if (*s [ || *s { || *s (){StackPush(st,*s);}else//右括号出栈比较{if (StackEmpty(st))//栈空右括号数量多{return false;}else{char top StackTop(st);StackPop(st);if ((top [ *s ! ])|| (top { *s ! })|| (top ( *s ! ))){return false;}}}s;}//运行到这里说明顺序都匹配//再进行数量匹配如果不为空则左括号多int ret StackEmpty(st);StackDestroy(st);return ret;//空为真不空为假
}