forked from timoncui/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify_Path.cpp
More file actions
39 lines (33 loc) · 771 Bytes
/
Simplify_Path.cpp
File metadata and controls
39 lines (33 loc) · 771 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
31
32
33
34
35
36
37
38
39
/*
Author: Timon Cui, [email protected]
Title: Simplify Path
Description:
Difficulty rating: Easy
Notes:
*/
class Solution {
public:
string simplifyPath(string path) {
deque<string> Q;
int pos = path.find('/');
while (pos + 1 < path.size()) {
int new_pos = path.find('/', pos + 1);
if (new_pos == string::npos) new_pos = path.size();
string p = path.substr(pos + 1, new_pos - pos - 1);
pos = new_pos;
if (p.size()) {
if (p == "..") {
if (!Q.empty()) Q.pop_back();
}
else if (p == ".") continue;
else Q.push_back(p);
}
}
string res = "";
while (!Q.empty()) {
res += "/" + Q.front();
Q.pop_front();
}
return res.size() ? res : "/";
}
};