#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
bool puanduan(string& s)
{
int n = s.size();
stack<char>sta;
for (int i = 0; i < n; i++)
{
sta.push(s[i]);
}
char last;
char temp=sta.top();
vector<char>result;
if (temp == '{' || temp == '[' || temp == '(')
{
return false;
}
while (!sta.empty())
{
temp = sta.top();
if (temp == '(' || temp == ')' || temp == '[' || temp == ']' || temp == '{' || temp == '}')
result.push_back(temp);
sta.pop();
}
reverse(result.begin(),result.end());
//for (int i = 0; i < result.size(); i++)
//cout << result[i];
int i = 0; int j = result.size() - 1;
if (result.size() % 2 != 0)
return false;
while(i<j)
{
if ((result[i] == '(' && result[j] == ')') || (result[i] == '[' && result[j] == ']') || (result[i] == '{' && result[j] == '}'))
{
result[i] = '0'; result[j] = '0';
i++; j--;
}
else
{
return false;
}
}
if(i>j)
return true;
}
int main()
{
string a = "([[sd]])skfh]";
cout << puanduan(a);
}
括号匹配(华为面试题我挂了)
于 2020-09-21 21:19:05 首次发布