Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 20. Valid Parentheses
參考資訊:
https://algo.monster/liteproblems/20
題目:

解答:
class Solution {
public:
bool isValid(string s) {
string st;
for (char& ch : s) {
if ((ch == '(') || (ch == '[') || (ch == '{')) {
st.push_back(ch);
}
else if (st.empty() ||
((st.back() == '(') && (ch != ')')) ||
((st.back() == '[') && (ch != ']')) ||
((st.back() == '{') && (ch != '}')))
{
return false;
}
else {
st.pop_back();
}
}
return st.empty();
}
};