class Solution {
public:
/**
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
if (s.length() % 2 != 0)
return false;
stack<char> st;
for (char& i : s) {
if (i == '(' || i == '[' || i == '{')
st.push(i);
else if (st.empty())
return false;
else {
if ((i == ']' && st.top() == '[') ||
(i == ')' && st.top() == '(') ||
(i == '}' && st.top() == '{'))
st.pop();
else
return false;
}
}
if (st.empty())
return true;
else
return false;
}
};

京公网安备 11010502036488号