Problem:
思路:
Solution (C++):
string simplifyPath(string path) {
string res, tmp;
vector<string> my_stack;
stringstream ss(path);
while (getline(ss, tmp, '/')) {
if (tmp == "" || tmp == ".") continue;
if (tmp == ".." && !my_stack.empty()) my_stack.pop_back();
else if (tmp != "..") my_stack.push_back(tmp);
}
for (auto str: my_stack) res += "/" + str;
return res.empty() ? "/" : res;
}
性能:
Runtime: 8 ms Memory Usage: 10.4 MB