#define MaxSize 10000
// 括号匹配
bool bracketCheck(const char str[], int length) {
// 声明一个顺序栈S, 静态分配内存空间
char data[MaxSize];
// 初始化栈
int top=-1;
for (int i = 0; i < length; i++) {
if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
// 左括号入栈
data[++top]=str[i];
}else{
if (top==-1) {
// 右括号单身
return false;
}
char topElem;
topElem=data[top--];
if (str[i]==')'&&topElem!='(') {
// 左右括号不匹配
return false;
}
if (str[i]==']'&&topElem!='[') {
// 左右括号不匹配
return false;
}
if (str[i]=='}'&&topElem!='{') {
// 左右括号不匹配
return false;
}
}
}
// 括号处理完毕, 判断栈内是否还有左括号: 左括号单身
return top==-1;
}
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
if (bracketCheck(s.c_str(), s.length())) {
return true;
}return false; ;
}
};