-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path71.cpp
More file actions
30 lines (30 loc) · 776 Bytes
/
Copy path71.cpp
File metadata and controls
30 lines (30 loc) · 776 Bytes
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
class Solution {
public:
string simplifyPath(string path) {
stack<string> s, t;
string p;
for (int i = 0; i <= path.size(); ++i) {
if (path[i] == '/' || i == path.size()) {
if (p == "..") {
if (!s.empty())
s.pop();
} else if (!p.empty() && p != ".")
s.push(p);
p.clear();
} else
p += path[i];
}
string ret = "/";
while (!s.empty()) {
t.push(s.top());
s.pop();
}
while (!t.empty()) {
ret += t.top() + "/";
t.pop();
}
if (ret.size() > 1)
ret.pop_back();
return ret;
}
};