-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 71
41 lines (35 loc) · 869 Bytes
/
Problem 71
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
class Solution {
public:
string simplifyPath(string path) {
stack<string> st;
string ans;
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())
{
ans = "/" + st.top() + ans;
st.pop();
}
if(ans.size() == 0)
return "/";
return ans;
}
};