-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify-path.cpp
43 lines (42 loc) · 1.09 KB
/
simplify-path.cpp
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
40
41
42
43
#include "header.h"
class Solution {
public:
string simplifyPath(string path) {
vector<string> parse;
string ans;
int i = 0, j = 0, n = path.size();
while(i < n) {
if(path[j] == '\/' || j == n) {
if(i != j) {
string temp = path.substr(i+1,j-i-1);
if(temp.size() == 0) {
}
else if(temp == ".") {
}
else if(temp == "..") {
if(parse.size() > 0) {
parse.pop_back();
}
}
else {
parse.push_back(temp);
}
i=j;
}
}
j++;
if(j > n) {
break;
}
}
int num = parse.size();
ans="\/";
for(int i = 0; i < num; i++) {
ans += parse[i];
if(i != num - 1) {
ans += "\/";
}
}
return ans;
}
};