class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
#include <stack>
bool isValid(string s) {
// write code here
stack<char>a;
for(char c:s){
if(c=='('||c=='['||c=='{')a.push(c);
else {
if(a.empty())return false;
else {
char top=a.top();
a.pop();
if((c==')'&&top!='(')||(c==']'&&
top!='[')||(c=='}'&&top!='{')){
return false;
}
}
}
}
if(a.empty())return true;
else return false;
}
};

京公网安备 11010502036488号