#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string& s) {
stack<char> st;
for (char a : s) {
if (a == '(') st.push(')');
else if (a == '{') st.push('}');
else if (a == ']') st.push(']');
else if (st.empty() || a != st.top()) return false;
else return true;
}
return st.empty();
}
};
int main()
{
string s = "(){}";
Solution S;
cout <<S.isValid(s) <<endl;
return 0;
}
C++ 有效括号
最新推荐文章于 2024-07-08 21:23:20 发布