每次都去寻找两个/
之间的指令,再把指令压入栈
.
表示当前目录 (跳过)..
表示上级目录 (若栈不为空,则出栈)a
进入名为 a 的子目录 (指令入栈)
最后从栈底依次输出指令,加上/
class Solution
{
public:
string simplifyPath(string path)
{
vector<string> vec;
for (auto i = path.begin(); i != path.end();)
{
++i;
auto j = find(i, path.end(), '/');
string temp = string(i, j);
if (temp != "" && temp != ".")
{
if (temp == "..")
{
if (!vec.empty())
vec.pop_back();
}
else
vec.push_back(temp);
}
i = j;
}
stringstream out;
if (vec.empty())
out << "/";
else
{
for (auto v : vec)
{
out << "/" << v;
}
}
return out.str();
}
};