-
Notifications
You must be signed in to change notification settings - Fork 0
/
71.cpp
48 lines (46 loc) · 1023 Bytes
/
71.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
string simplifyPath(string path) {
string res;
queue<string> operations;
string partial;
int i = 0;
while (i < path.size()) {
while (i < path.size() && path[i] != '/') {
partial += path[i];
i++;
}
if (!partial.empty()) {
operations.push(partial);
partial.clear();
}
string str(1, '/');
operations.push(str);
i++;
}
while (!operations.empty()) {
string next = operations.front();
if (next == "/") {
if (res.empty() || res.back() != '/') {
res += next;
}
} else if (next == "..") {
int i = res.size() - 2;
while (i >= 0 && res[i] != '/') {
i--;
}
if (i >= 0) {
res.erase(i);
}
} else if (next == ".") {
} else {
res += next;
}
operations.pop();
}
if (res.size() > 1 && res[res.size() - 1] == '/') {
res.pop_back();
}
return res;
}
};