leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0071.cpp (736B)
0 class Solution {
1 public:
2 string simplifyPath(string path) {
3 deque<string> dq;
4 int start = 0;
6 path.push_back('/');
7 for (int i = 0; i < path.size(); i++) {
8 if (path[i] != '/')
9 continue;
10 else {
11 string s = path.substr(start, i - start);
12 if (s == "..") {
13 if (!dq.empty()) dq.pop_back();
14 } else if (!s.empty() && s != ".")
15 dq.push_back(s);
16 start = i + 1;
17 }
18 }
20 string res = "";
21 while (!dq.empty()) {
22 res += "/" + dq.front();
23 dq.pop_front();
24 }
25 return res.empty() ? "/" : res;
26 }
27 };