文章
23
粉丝
0
获赞
118
访问
3.6k
当输入字符串以右括号开头时(如")"或"]"),栈为空状态下执行S.top()会导致运行时错误
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> S;
string s;
cin>>s;
int len=s.length();
char x;
for(int i=0; i<len; i++)
{
if(s[i]=='(')
S.push(s[i]);
if(s[i]=='[')
S.push(s[i]);
if(s[i]==')' || s[i]==']')
{
x=S.top();
if((s[i]==')'&&x=='(') || (s[i]==']'&&x=='['))
S.pop();
else
&nbs...
登录后发布评论
暂无评论,来抢沙发