```cpp #include <iostream> #include <stack> using namespace std; class Solution { public: bool isValid(string s) { stack<char> st; for (char c : s) { if (c == '(' || c == '[' || c == '{') { st.push(c); } else { if (st.empty()) { return false; } char top = st.top(); st.pop(); if ((c == ')' && top!= '(') || (c == ']' && top!= '[') || (c == '}' && top!= '{')) { return false; } } } return st.empty(); } }; int main(){ Solution s; string str; getline(cin, str); bool res = s.isValid(str); cout<<res; return 0; } ``` 额外需要注意的逻辑点: 1. 注意边界情况,比如输入为空字符串时的处理。 2. 对于栈的操作要确保没有越界。 3. 考虑不同括号组合的各种可能情况,确保代码的正确性和完整性。 [2025-03-07 12:50:41 | AI写代码神器 | 278点数解答]