-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0071_Simplify_Path.cpp
More file actions
61 lines (56 loc) · 1.69 KB
/
Copy path0071_Simplify_Path.cpp
File metadata and controls
61 lines (56 loc) · 1.69 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
string simplifyPath(string path) {
int i, n = path.size();
vector<string> stack;
string output = "/";
for(i = 0; i < n; i++){
while(i < n && path[i] == '/') i++;
if(i >= n) break;
// cur path
if(path[i] == '.' && (i + 1 >= n || path[i + 1] == '/')) continue;
// last path, pop back
if(path[i] == '.' && path[i + 1] == '.' && (i + 2 >= n || path[i + 2] == '/')){
if(stack.size() > 0){
//cout << "POP " << stack.back() << endl;
stack.pop_back();
}
i++;
}
// directory
else{
string tmp = "";
while(i < n && path[i] != '/')
tmp += path[i++];
stack.push_back(tmp);
//cout << "PUSH " << stack.back() << endl;
i--;
}
}
// tidy directory in the stack
for(i = 0; i < stack.size(); i++){
output += stack[i];
if(i < stack.size() - 1)
output += '/';
}
return output;
}
};
int main(){
Solution solve;
//string input = "/home//";
//string input = "/../";
//string input = "/home//foo/";
//string input = "/a/./b/../../c/";
//string input = "/a/../../b/../c//.//";
string input = "/a//b////c/d//././/..";
//string input = "/...";
//string input = "/.b";
cout << "Input: " << input << endl;
cout << "Output: " << solve.simplifyPath(input) << endl;
return 0;
}