-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path71.cpp
More file actions
48 lines (41 loc) · 1.04 KB
/
71.cpp
File metadata and controls
48 lines (41 loc) · 1.04 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
// Problem : 71. Simplify Path
// Link : https://leetcode.com/problems/simplify-path/
#include <iostream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
class Solution {
public:
string simplifyPath(string path) {
stack<string> st;
string return_path;
for (int i = 0; i < path.size(); ++i) {
if (path[i] == '/')
continue;
string temp;
while (i < path.size() && path[i] != '/') {
temp += path[i];
++i;
}
if (temp == ".")
continue;
else if (temp == "..") {
if (!st.empty())
st.pop();
} else
st.push(temp);
}
while (!st.empty()) {
return_path = "/" + st.top() + return_path;
st.pop();
}
if (return_path.size() == 0)
return "/";
return return_path;
}
};
int main() {
Solution ob;
cout << ob.simplifyPath("/..hidden");
return 0;
}