美文网首页
20. Valid Parentheses

20. Valid Parentheses

作者: larrymusk | 来源:发表于2017-11-30 21:44 被阅读0次
    #define STACK_SIZE  10000
    struct stack{
            char a[STACK_SIZE];
            int top;
    };
    void push(struct stack *s, char c)
    {
            if(s->top == STACK_SIZE-1)
                    return;
            else{
                    s->top++;
                    s->a[s->top] = c;
            }
    
    }
    
    char pop(struct stack *s)
    {
            if(s->top == -1)
                    return -1;
            else{
                
                    return s->a[s->top--];
            }
    }
    
    bool isValid(char* s) {
            struct stack stack;
            stack.top = -1;
            char c;
            int tag = 0;
    
            while(*s != '\0'){
                    printf("%c tag = %d\n", *s, tag);
                    if(*s == '(' || *s == '{' || *s == '['){
                            push(&stack, *s);
                            s++;
                            tag++;
                    }
                    else if(*s == ')' || *s == '}' || *s == ']'){
                            c = pop(&stack);
                            printf("c = %c\n", c);
                            if(c == '(' && *s == ')')
                                    tag--,s++;
                            else if(c == '{' && *s == '}')          
                                    tag--,s++;
                            else if(c == '[' && *s == ']')
                                    tag--,s++;
                            else
                                    return false;
                    }else
                            s++;
    
            }
     if(tag == 0)
                return true;
        else
                return false;
    }
    

    相关文章

      网友评论

          本文标题:20. Valid Parentheses

          本文链接:https://www.haomeiwen.com/subject/kombbxtx.html