-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify_Path.cpp
More file actions
37 lines (30 loc) · 813 Bytes
/
Copy pathSimplify_Path.cpp
File metadata and controls
37 lines (30 loc) · 813 Bytes
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
class Solution {
public:
string simplifyPath(string path) {
if(path == "") return path;
stack<string> st;
int n = path.size()-1;
int i = 0;
int j;
string t;
while(i <= n){
j = i+1;
while(j <= n && path[j] != '/') j++;
t = path.substr(i, j-i);
if(t == "/" || t == "/."){
}else if(t == "/.."){
if(!st.empty()) st.pop();
}else{
st.push(t);
}
i = j;
}
if(st.empty()) return string("/");
string result;
while(!st.empty()){
result = st.top() + result;
st.pop();
}
return result;
}
};