Simplify Path
Desicription
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Solution
代码语言:javascript复制class Solution {
public:
string simplifyPath(string path) {
vector<string> nameVect;
string name;
path.push_back('/');
for(int i=0;i<path.size();i ){
if(path[i]=='/'){
if(name.size()==0)continue;
if(name==".."){
if(nameVect.size()>0)nameVect.pop_back();
}else if(name=="."){
}else{
nameVect.push_back(name);
}
name.clear();
}else{
name.push_back(path[i]);
}
}
string result;
if(nameVect.empty())return "/";
for(int i=0;i<nameVect.size();i ){
result.append("/" nameVect[i]);
}
return result;
}
};