用栈模拟
class Solution {
public:
string simplifyPath(string path) {
vector<string> sta;
string s;
for (auto &x : path)
if (x == '/') x = ' ';
stringstream ss(path);
while (ss >> s) {
if (s == "..") {
if (!sta.empty()) sta.pop_back();
}
else if (s != ".") sta.push_back(s);
}
s = "";
if (sta.empty()) return "/";
for (auto &x : sta)
s += "/" + x;
return s;
}
};