Given
an absolute path for a file (Unix-style), simplify it.
class Solution {private:
int index;
string path;
string getpath()
{
index++;
string newpath=
"";
while(index<path.length() && path[index]!=
‘/‘)
{
newpath=newpath+path[index];
index++;
}
return newpath;
}
public:
string simplifyPath(
string path)
{
this->path=path;
stack<
string> stk;
index=
0;
while(index<path.size())
{
string newpath=getpath();
if(newpath==
"..")
{
if(stk.empty()==
false)
stk.pop();
continue;
}
if(newpath==
"." || newpath==
"")
continue;
stk.push(newpath);
}
string result=
"";
while(stk.empty()==
false)
{
result=
‘/‘+stk.top()+result;
stk.pop();
}
if(result.length()==
0) result=
"/";
return result;
}
};