首页 > 其他分享 >71. Simplify Path

71. Simplify Path

时间:2023-01-11 12:00:42浏览次数:43  
标签:&& string res else Simplify 71 Path path empty


这道题用栈特别方便

string simplifyPath(string path) {
stack<string>a_path;
path+='/';
for(int i=1;i<path.length();++i){
if(path[i]=='/')continue;
else if(path[i]=='.'&&path[i+1]=='.'&&path[i+2]=='/'){
if(!a_path.empty())a_path.pop();
}
else if(path[i]=='.'&&path[i+1]=='/')continue;
else{
string temp_path="/";
while(path[i]!='/'){
temp_path+=path[i];
++i;
}
a_path.push(temp_path);
}
}
string res_path="";
while(!a_path.empty()){
res_path=a_path.top()+res_path;
a_path.pop();
}
if(res_path.empty())res_path+='/';
return res_path;
}
```


标签:&&,string,res,else,Simplify,71,Path,path,empty
From: https://blog.51cto.com/u_14035071/6001915

相关文章