题解 | 括号配对问题
括号配对问题
https://www.nowcoder.com/practice/57260c08eaa44feababd05b328b897d7
#include <ios>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<char> st;
int main() {
string str;
bool result{true};
cin >> str;
for(int i=0;i<str.size();i++)
{
if (str[i] == '(' || str[i] == '[')
{
st.push(str[i]);
}
else if(str[i] == ')')
{
if(st.empty())
{
result = false;
break;
}
else
{
if(st.top() == '(')
{
st.pop();
}
else
{
result = false;
break;
}
}
}
else if(str[i] == ']')
{
if(st.empty())
{
result = false;
break;
}
else
{
if(st.top() == '[')
{
st.pop();
}
else
{
result = false;
break;
}
}
}
}
cout << boolalpha <<result;
return 0;
}
// 64 位输出请用 printf("%lld")
查看11道真题和解析