//知识点:栈 stack
//特点:先进后出
#include<iostream>
using namespace std;
#include<stack>
#include<string>
class Solution {
public:
bool isValid(string s) {
for (char a : s)
{
if (a == '(' || a == '[' || a == '{')
{
st.push(a);
}
else//右半面括号进行匹配并判断
{
if (st.size() == 0)
{
return false;
}
else
{
char temp = st.top();
st.pop();
if (a == ')')
{
if (temp != '(') return false;
}
else if (a == ']')
{
if (temp != '[') return false;
}
else if (a == '}')
{
if (temp != '{') return false;
}
}
}
}
if (st.size() == 0)
{
return true;
}
else
{
return false;
}
}
private:
stack<char> st;
};
void test01() {
string m_s = "[(({})}]";
Solution S;
bool jd = S.isValid(m_s);
cout << jd << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
08-11
317