-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplify-path.cpp
42 lines (39 loc) · 878 Bytes
/
simplify-path.cpp
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
/* https://leetcode.com/problems/simplify-path/ */
class Solution {
public:
string simplifyPath(string path) {
stack<string>st;
for(int i=0;i<path.length();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);
}
}
string res;
while(!st.empty())
{
res="/"+st.top()+res;
st.pop();
}
if(res.size()==0)
{
return "/";
}
return res;
}
};