题解 | 括号配对问题

括号配对问题

https://www.nowcoder.com/practice/57260c08eaa44feababd05b328b897d7

#include <stdio.h>

int main() {
    char s[100000000];char a[10000000];int top=-1;int flag=1;
    scanf("%s",&s);
    for(int i=0;s[i]!='\0';i++)
    {
        if(s[i]!='['&&s[i]!=']'&&s[i]!='('&&s[i]!=')')
        {
            continue;
        }
        else if(s[i]=='['||s[i]=='(')
        {
             a[++top]=s[i];
           
        }
        else {if(top==-1)
        {flag=0;
            break;
        }
        if(s[i]==')'&&a[top]=='(')
        {
           top--;
        }
        
        else if(s[i]==']'&&a[top]=='[')
        {
            top--;
        }
        else{
            break;
        }
        }
    }
    int size=top+1;
    if(size==0&&flag==1)
    {
        printf("true");
    }
    else if(size!=0){
        printf("false");
    }
    else if(flag==0)
    {
        printf("false");
    }
    return 0;
}

全部评论

相关推荐

哞客37422655...:你猜为什么福利这么好还得一直追着你问
点赞 评论 收藏
分享
02-12 01:30
已编辑
四川文理学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务