Share my c++ code. Thinking the corner case is the most difficult for this problem.


  • 0
    class Solution {
    public:
    string simplifyPath(string path) {
        vector<string> dirs;
        size_t start = 0;
        // using find, so I would like the last char is slash to simplify the code
        path.push_back('/');
        size_t found = path.find_first_of("/");
        while (found!=std::string::npos)
        {
            if(start != found){
                string sub = path.substr(start, found-start);
                if( sub == "." || sub.empty())
                {}
                else if( sub == "..") {
                    if(!dirs.empty()){
                        dirs.pop_back();
                    }
                }
                else
                    dirs.push_back(sub);
            }
            start = found+1;
            found=path.find_first_of("/",start);
        }
        string res;
        // abs path starts with slash.
        res.push_back('/');
        int size=dirs.size();
        for(int i=0; i<size; ++i){
            // skip the first one.
            if(i) res.push_back('/');
            res += dirs[i];
        }
        return res;
    }
    

    };


Log in to reply
 

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