Simple and clean C++ code


  • 1
    string simplifyPath(string path) {
        istringstream iss(path);
        vector<string> res;
        string tok, result;
        while( getline(iss, tok, '/') ) {
            if( tok != "." && tok != ".." && !tok.empty()) res.push_back(tok);
            else if( tok == ".." ) if( !res.empty()) res.pop_back();
        }
        for( string s : res ) result.append("/" + s);
        return result.empty() ? "/" : result;
    }

  • 0

    Using stack to store directories is brilliant


Log in to reply
 

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