C++ string stream solution


  • 0
    J
    class Solution {
    public:
        string simplifyPath(string path) {
            stringstream is(path+"/");
            stack<string> stk;
            string token;
            while(getline(is,token,'/'))
            {
                if(token==""||token==".")continue;
                if(token=="..")
                        {if(!stk.empty()) stk.pop();}
                else
                    stk.push(token);
            }
            string result;
            while(!stk.empty()) {result ="/"+stk.top()+result;stk.pop();}
            return result==""?"/":result;;
        }
    };
    

Log in to reply
 

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