My 6 ms C++ solution without "SPLIT"


  • 0
    K
        string simplifyPath(string path) {
            string location = "/";
            list<string> paths;
            for (auto c : path + '/') {
                if (c == '/') {
                    if (location.compare("/.") && location.compare("/")) {
                        if (location.compare("/..") == 0){
                            if (!paths.empty()) paths.pop_back();
                        }
                        else paths.push_back(location);
                    }
                    location = "/";
                } else {
                    location += c;
                }
            }
            string ret;
            for (auto &s : paths)   ret += s;
            return ret.empty() ? "/" : ret;
        }
    

Log in to reply
 

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