Skip to content

Commit fe85957

Browse files
committed
Simplify Path: stack.
1 parent 9921890 commit fe85957

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Medium/071_Simplify-Path.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../Header.h"
2+
3+
using namespace std;
4+
string simplifyPath(string path) {
5+
if (path[path.length()-1] != '/') path = path + "/";
6+
stack<string> s;
7+
int bg = 1;
8+
for (int i = 1; i < path.length();i++) {
9+
if (path[i] == '/') {
10+
if (i-bg) {
11+
string q = path.substr(bg, i-bg);
12+
if (q == "."){}
13+
else if (q == "..") {
14+
if (!s.empty()) s.pop();
15+
} else s.push(q);
16+
}
17+
bg = i+1;
18+
}
19+
}
20+
if (s.empty()) return "/";
21+
string res = "";
22+
while (!s.empty()) {
23+
res = "/" + s.top() + res;
24+
s.pop();
25+
}
26+
return res;
27+
}
28+
int main(int argc, char const *argv[])
29+
{
30+
return 0;
31+
}

0 commit comments

Comments
 (0)