-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path71-Simplify-Path.cpp
More file actions
43 lines (42 loc) · 1.03 KB
/
71-Simplify-Path.cpp
File metadata and controls
43 lines (42 loc) · 1.03 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
class Solution {
public:
string simplifyPath(string path){
string ans;
stack<string>st;
int i = 0;
while(i < path.size()){
int start = i;
int end = i+1;
while(end < path.size() && path[end] != '/'){
end++;
}
i = end;
string tempstr = path.substr(start, end-start);
if(tempstr == \/.\ || tempstr == \/\){
continue;
}
else if(tempstr == \/..\){
if(!st.empty()){
st.pop();
}
}
else {
st.push(tempstr);
}
}
stack<string> temp;
while(!st.empty()){
temp.push(st.top());
st.pop();
}
string s = temp.empty() ? \/\ : \\;
while(!temp.empty()){
ans = ans + temp.top();
temp.pop();
}
if(ans.empty()){
return \/\;
}
return ans;
}
};