Simplify Path

string simplifyPath(string path) {

vector<string> s;//vector当做栈

string dir;

for(auto i=path.begin();i!=path.end();){

i++;

auto j = find(i,path.end(),'/');

dir = string(i,j);

if(!dir.empty() && dir!="."){

if(dir==".."){

if(!s.empty())

s.pop_back();

}else{

s.push_back(dir);

}

}

i=j;

}

stringstream out;

if(s.empty())

out<<"/";

else{

for(auto i:s){

out<<"/"<<i;

}

}

return out.str();

}

Last updated

Was this helpful?