forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution071.cpp
More file actions
56 lines (47 loc) · 948 Bytes
/
solution071.cpp
File metadata and controls
56 lines (47 loc) · 948 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Simplify Path
*
* cpselvis([email protected])
* Nov 28th, 2016
*/
#include<iostream>
#include<stack>
using namespace std;
class Solution {
public:
string simplifyPath(string path) {
stack<string> st;
string ret = "";
for (int i = 0; i < path.size(); )
{
// Jump when meet character /
while (path[i] == '/' && i < path.size())
i ++;
string s = "";
while (path[i] != '/' && i < path.size())
{
s = s + path[i];
i ++;
}
if (s == ".." && !st.empty())
st.pop();
else if (s != "" && s != "." && s != "..")
st.push(s);
}
if (st.empty())
ret = "/";
while (!st.empty())
{
ret = "/" + st.top() + ret;
st.pop();
}
return ret;
}
};
int main(int argc, char **argv)
{
Solution s;
cout << s.simplifyPath("/..") << endl;
cout << s.simplifyPath("/home/") << endl;
cout << s.simplifyPath("/a/./b/../../c/") << endl;
}