0%

71. Simplify Path

O(n) n是路径层数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
istringstream input(path);
string s;
while (getline(input, s, '/')) {
if (s.empty() || s == ".") continue;
if (s == "..") {
if (!v.empty()) {
v.pop_back();
}
} else {
v.push_back(s);
}
}
if (v.empty()) return "/";
string res;
for (const auto &s : v) {
res = res + "/" + s;
}
return res;
}
};