#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
char leftOf(const char& c)
{
if (c == '}')return '{';
if (c == ')')return '(';
if (c == ']')return '[';
return '\0';
}
bool isValid(const string& str)
{
stack<char > left;
for (char c : str)
{
if (c == '(' || c == '{' || c == '[')
left.push(c);
else if (!left.empty() && (c == ')'|| c == '}' || c==']'))
{
if (leftOf(c) == left.top())
{
left.pop();
}
else {
return false;
}
}
else if (left.empty() && (c == ')' || c == '}' || c == ']'))
{
return false;
}
}
return left.empty();
}
int main()
{
string str = "}";
cout << isValid(str);
return 0;
}
主要判断:
string str = “[dfdf({fdfdfdfd})]”;