3ms C solution


  • 0
    D
    bool isValid(char* s) {
        int len = strlen(s), size = 0;
        if(len%2!=0) return false;
        int check[150] = {NULL};
        check[')'] = '(';
        check['}'] = '{';
        check[']'] = '[';
        char* stack = (char*)malloc(sizeof(char)*len/2);
        memset(stack,0,sizeof(char)*len/2);
        for(int i=0;i<len;i++){
            if(*(s+i)==')'||*(s+i)==']'||*(s+i)=='}'){
                if(size<=0) return false;
                size -= 1;
                if(stack[size]!=check[*(s+i)]) return false;
            }
            else stack[size++] = *(s+i);
        }
        if(size!=0) return false;
        return true;
    }
    

Log in to reply
 

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