forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path071.Simplify-Path.cpp
More file actions
39 lines (36 loc) · 822 Bytes
/
Copy path071.Simplify-Path.cpp
File metadata and controls
39 lines (36 loc) · 822 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
class Solution {
public:
string simplifyPath(string path)
{
int i = 0;
vector<string>q;
while (i+1<path.size())
{
int j = path.find("/", i+1);
if (j==-1)
{
q.push_back(path.substr(i+1));
break;
}
else
{
q.push_back(path.substr(i+1, j-i-1));
i = j;
}
}
vector<string>p;
for (auto s: q)
{
if (s=="." || s=="") continue;
else if (s=="..")
{
if (p.size()>0) p.pop_back();
}
else p.push_back(s);
}
string ret;
for (auto s:p)
ret+='/'+s;
return ret == ""? "/":ret;
}
};