Skip to content

Commit 3bd4808

Browse files
authored
Create 071.Simplify-Path.cpp
1 parent 3fabc2c commit 3bd4808

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
string simplifyPath(string path)
4+
{
5+
int i = 0;
6+
vector<string>q;
7+
while (i+1<path.size())
8+
{
9+
int j = path.find("/", i+1);
10+
if (j==-1)
11+
{
12+
q.push_back(path.substr(i+1));
13+
break;
14+
}
15+
else
16+
{
17+
q.push_back(path.substr(i+1, j-i-1));
18+
i = j;
19+
}
20+
}
21+
22+
vector<string>p;
23+
for (auto s: q)
24+
{
25+
if (s=="." || s=="") continue;
26+
else if (s=="..")
27+
{
28+
if (p.size()>0) p.pop_back();
29+
}
30+
else p.push_back(s);
31+
}
32+
33+
string ret;
34+
for (auto s:p)
35+
ret+='/'+s;
36+
return ret == ""? "/":ret;
37+
38+
}
39+
};

0 commit comments

Comments
 (0)