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

LeetCode 71. Simplify Path

时间:2022-10-18 14:04:10浏览次数:68  
标签:paths 71 pos else Simplify str path LeetCode string

​题目​

字符串问题

class Solution {
public:
string simplifyPath(string path) {

string paths[10005];
int pos=0;
paths[0]="/";
string str="";
for(int i=0;i<path.length();i++)
{
if(i==0&&path[i]=='/')
continue;

if(path[i]=='/')
{
if(str=="")
continue;
if(str=="..")
{
if(pos>0){
pos--;
}
str="";
continue;
}
else if(str==".")
{
str="";
continue;
}
else
{
str+='/';
paths[++pos]=str;
str="";
}

}
else {

str+=path[i];
}
}
if(str=="..")
{
if(pos>0){
pos--;
}
}
else if(str!=""&&str!=".")
{
str+='/';
paths[++pos]=str;
}

if(pos!=0){

paths[pos]=paths[pos].substr(0,paths[pos].length()-1);
}

string ans="";
for(int i=0;i<=pos;i++)
{
ans+=paths[i];
}
return ans;

}
};



标签:paths,71,pos,else,Simplify,str,path,LeetCode,string
From: https://blog.51cto.com/u_15834522/5766282

相关文章