【发布时间】:2020-11-10 17:04:12
【问题描述】:
GFG 括号检查器在测试用例“[][]”中显示错误答案,但是当我使用自定义测试用例时,它显示正确答案。我的代码没有出错。 这是我的 C++ 代码。
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
string s;
cin >> s;
int l = s.length();
stack<char> stq;
int flag = 0;
for (int i = 0; i < l; i++){
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
stq.push(s[i]);
else if (!stq.empty() && s[i] == ')' && stq.top() == '(')
stq.pop();
else if (!stq.empty() && s[i] == ']' && stq.top() == '[')
stq.pop();
else if (!stq.empty() && s[i] == '}' && stq.top() == '{')
stq.pop();
else{
cout << "not balanced" << endl;
break;
flag = 1;
}
}
if (flag == 0){
if (stq.empty())
cout << "balanced" << endl;
else
cout << "not balanced" << endl;
}
}
return 0;
}
问题的链接是https://practice.geeksforgeeks.org/problems/parenthesis-checker/0#
【问题讨论】: