(AC) solution C use stack


  • 0
    1

    struct pool{
    char val;
    struct pool* pre;
    struct pool* next;
    };

    char pop(struct pool**);
    struct pool* push(struct pool*,char);
    char juge(char);

    bool isValid(char* s) {
    int len=strlen(s);
    if((len%2) || len==0)
    return false;

    struct pool* arr=NULL;
    for(int i=0;i<len;i++)
    {
        char t=juge(s[i]);
        if(t!=-1){
            arr=push(arr,t);
        }
        else
        {
            if(s[i]!=pop(&arr))
                return false;
        }
    }
    if(arr!=NULL)return false;
    return true;
    

    }

    struct pool* push(struct pool* point,char val)
    {

    if(point==NULL)
    {
        point=malloc(sizeof(struct pool));
        point->val=val;
        point->pre=NULL;
        point->next=NULL;
        return point;
    }
    struct pool* tmp=point;
    point->pre=malloc(sizeof(struct pool));
    point=point->pre;
    point->next=tmp;
    point->val=val;
    return point;
    

    }
    char pop(struct pool** point)
    {
    if((*point)==NULL)return -1;
    char s=(point)->val;
    struct pool
    tmp=(*point);
    if((*point)->next)
    {
    (*point)=(*point)->next;
    }
    else
    {
    (*point)=NULL;
    }
    free(tmp);
    printf("pop=%c\n",s);
    return s;
    }

    char juge(char s)
    {
    char ans=-1;
    switch(s)
    {
    case '(':
    ans=')';
    break;
    case '[':
    ans=']';
    break;
    case '{':
    ans='}';
    break;
    }
    return ans;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.