Why the output is diffrrent with my local output and wrong when i use free() in c


  • 0
    B
    #include <stdlib.h>
    #include <string.h>
    
    int longestValidParentheses(char* s) {
        if(s==NULL){
            return 0;
        }
        int maxLen = 0;
        int curLen = 0;
    
        int sLen = strlen(s);
    //    char* ps = s;
    //    while(*ps){
    //        sLen++;
    //        ps++;
    //    }
    
        char* stack = (char*)malloc((sLen+1)*sizeof(char));
        if(stack==NULL){
            return -1;
        }
        char* pStack = stack;
        //pStack[sLen] = '\0';
        char* stackEnd = stack+sLen;
        char* stackPre = stack;;
    
        while(*s){
            switch(*s){
            case '(':
                *pStack = *s;
                pStack++;
                break;
            case ')':
                if(pStack!=stack && *(pStack-1)=='('){
                    *(pStack-1) = '*';
                    if(pStack-1!=stack){
                        stackPre = pStack-2;
                        while(stackPre!=stack && *stackPre=='*'){
                            stackPre--;
                        }
                    }
                }else if(*(pStack-1)=='*'){
                    if(*stackPre=='('){
                        *stackPre = '*';
                        while(stackPre!=stack && *stackPre=='*'){
                            stackPre--;
                        }
                    }else if(pStack!=stackEnd){
                        *pStack = ')';
                        pStack++;
                    }
                }else if(pStack!=stackEnd){
                    *pStack = ')';
                    pStack++;
                }
                break;
            }
            s++;
        }
        pStack = stack;
        int i=0;
        //while(*pStack){
        for(i=0; i<sLen; i++){
            if(pStack[i]=='*'){
                curLen += 2;
            }else{
                if(curLen>maxLen){
                    maxLen = curLen;
                }
                curLen = 0;
            }
            //pStack++;
        }
        if(curLen>maxLen){
            maxLen = curLen;
        }
    
        //free(stack);//!!!!!!!!!!!!!!!!!!!!!! here,i can not use this !!!!!!!!!!!!!!!!!!!
        return maxLen;
    }

Log in to reply
 

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