My simple C++ code


  • 4
    H
    string simplifyPath(string path) {
            const char* p = path.c_str();
            string res = "/";
            vector<string> full_path;
            char* addr = strtok((char*)p, "/");
            int cur = 0, size = 0;
            while(addr) {
                if(strcmp(addr, ".") == 0) {
                }       
                else if(strcmp(addr, "..") == 0) {
                    cur > 0 ? cur-- : cur;
                }    
                else {
                    if(cur == size) {
                        full_path.push_back(addr);
                        cur++;
                        size++;
                    }
                    else {
                        full_path[cur++] = addr;
                    }
                }     
                addr = strtok(NULL, "/");
            }
            for(int i=0; i<cur; i++) {
                res = res + full_path[i] + "/";
            }
            return res.size() == 1 ? res : res.substr(0, res.size() - 1);
    }

  • 0
    5

    I still think the strtok function is somehow "anti-human"...


Log in to reply
 

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