LeetCode | Valid Parentheses(括号匹配)


Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

题意很简单,就是判断是否匹配。通过栈来实现,如果左括号等,就入栈,如果右括号类,就出栈并判断是否匹配。当全部匹配完了,还要判断栈是否为空,不为空,也代表匹配不成功!

void ValidParentheses(char *str)
{
    if(str == NULL){
        printf("empty\n");
        return ;
    }

    char ch;
    SqStack S;
    int len = strlen(str);
    InitStack(&S);
    if(len % 2){
        printf("odd\n");
        return;
    }
    for(int i = 0;i < len;++i){
        if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
            Push(&S,str[i]);
        }else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
            if(IsEmpty(S)){ //在POP函数中已经设置了错误判断,如果为空,就报错并退出循环。
                printf("invalid!\n");   //但是直接退出程序不好,所有就提前判断是否为空,为空就报错,退出函数即可。
                return;
            }
            ch = Pop(&S);
            if(!Match(ch,str[i])){
                printf("invalid\n");
                return ;
            }
        }else{  //处理输入其他字符的情况
            printf("the char doesn't correct!\n");
            return ;
        }
    }
    if(!IsEmpty(S)){
        printf("doesn't match!\n");
    }else
        printf("match!\n");
}

完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACKSIZEINIT 50
#define INCREMENT 10

typedef struct{
    char *base;
    char *top;
    int stacksize;
}SqStack;


void InitStack(SqStack *S)
{
    S->base = (char *)malloc(STACKSIZEINIT * sizeof(char));
    if(S->base == NULL){
        printf("malloc error\n");
        exit(1);
    }
    S->top = S->base;
    S->stacksize = STACKSIZEINIT;
}

void Push(SqStack *S,char ch)
{
    if(S->top - S->base >= S->stacksize){
        S->base = (char *)realloc(S->base,(S->stacksize + INCREMENT)*sizeof(char));
        if(S->base == NULL){
            printf("malloc error\n");
            exit(1);
        }
        S->top = S->base + S->stacksize;
        S->stacksize += INCREMENT;
    }
    *(S->top++) = ch;
}



int IsEmpty(SqStack S)
{
    return S.base == S.top;
}

char Pop(SqStack *S)
{
    char ch;
    if(IsEmpty(*S)){
        printf("stack empty\n");
        exit(1);
    }
    ch = *(--S->top);
    return ch;
}

int Match(char a,char b)
{
    switch(a){
    case '(':
        return b == ')';
    case '[':
        return b == ']';
    case '{':
        return b == '}';
    default :
        return 0;
    }
}

void ValidParentheses(char *str)
{
    if(str == NULL){
        printf("empty\n");
        return ;
    }

    char ch;
    SqStack S;
    int len = strlen(str);
    InitStack(&S);
    if(len % 2){
        printf("odd\n");
        return;
    }
    for(int i = 0;i < len;++i){
        if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
            Push(&S,str[i]);
        }else if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
            if(IsEmpty(S)){ //在POP函数中已经设置了错误判断,如果为空,就报错并退出循环。
                printf("invalid!\n");   //但是直接退出程序不好,所有就提前判断是否为空,为空就报错,退出函数即可。
                return;
            }
            ch = Pop(&S);
            if(!Match(ch,str[i])){
                printf("invalid\n");
                return ;
            }
        }else{  //处理输入其他字符的情况
            printf("the char doesn't correct!\n");
            return ;
        }
    }
    if(!IsEmpty(S)){
        printf("doesn't match!\n");
    }else
        printf("match!\n");
}


int main()
{
    char buf[20];

    while(scanf("%s",buf)!=EOF){
        ValidParentheses(buf);
    }
    return 0;
}

点赞