The adherent code! in C


  • 0
    Y
        #define MAX 1000
        char* simplifyPath(char *path){
        char *r = malloc(MAX);
        char str[MAX];
        char stack[MAX][MAX];
        int top=0,j,i=0;
        while(1){
                while(path[i]=='/')
                        i++;
                j = 0;
                while(path[i] && path[i]!='/')
                        str[j++] = path[i++];
                str[j] = 0;
                if(j!=0){
                        if(!strcmp(str,"..")){
                                if(top!=0)
                                        top--;
                        }
                        else if(strcmp(str,"."))
                                strcpy(stack[top++],str);
                }
                else
                        break;
        }
        strcpy(r,"/");
        for(i=0;i<top;i++){
                strcat(r,stack[i]);
                if(i!=top-1)
                        strcat(r,"/");
        }
        return r;
    

    }


Log in to reply
 

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