leetcode 71

通过getline将"/"分隔取得其中的字符串进行判断,注意..是返回上一级

string simplifyPath(string path) {
        stringstream ss(path);
        string str;
        string res;
        stack<string> s;
        while(getline(ss,str,'/'))
        {
            if(str==""||str==".")continue;
            else if(str==".."){
                if(!s.empty())s.pop();
            }
            else s.push(str);
        }
        while(!s.empty())
        {
            res="/"+s.top()+res;
            s.pop();
        }
        return res==""?"/":res;
    }

猜你喜欢

转载自blog.csdn.net/TempterCyn/article/details/84200420