The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.
true
class Solution {
public:
/**
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
int len=s.size();
stack <char> sta;
for (int i=0;i<len;i++){
if (s[i]==‘(‘)sta.push(‘)‘);
else if (s[i]==‘[‘) sta.push(‘]‘);
else if (s[i]==‘{‘)sta.push(‘}‘);
else if (sta.empty())return false;
else if (sta.top()!=s[i])return false;
else sta.pop();
}
return sta.empty();
}
};
原文:https://www.cnblogs.com/hrnn/p/13439712.html