class Solution {
public:
string simplifyPath(string path) {
stack<string> s;
string tmp;
for(int i=0;i<path.size();i++)
{
if(path[i]=='/')
{
if(tmp=="..")
{
if(!s.empty())
s.pop();
}
else if(tmp!="."&&tmp!="")
{
s.push(tmp);
}
tmp="";
}
else tmp+=path[i];
}
if(tmp=="..")
{
if(!s.empty())
s.pop();
}
else if(tmp!="."&&tmp!="") //else if选择语句
{
s.push(tmp);
}
string ret="";
if(s.empty())
return "/";
while(!s.empty())
{
ret='/'+s.top()+ret;
s.pop();
}
return ret;
}
};
public:
string simplifyPath(string path) {
stack<string> s;
string tmp;
for(int i=0;i<path.size();i++)
{
if(path[i]=='/')
{
if(tmp=="..")
{
if(!s.empty())
s.pop();
}
else if(tmp!="."&&tmp!="")
{
s.push(tmp);
}
tmp="";
}
else tmp+=path[i];
}
if(tmp=="..")
{
if(!s.empty())
s.pop();
}
else if(tmp!="."&&tmp!="") //else if选择语句
{
s.push(tmp);
}
string ret="";
if(s.empty())
return "/";
while(!s.empty())
{
ret='/'+s.top()+ret;
s.pop();
}
return ret;
}
};