-
Notifications
You must be signed in to change notification settings - Fork 0
/
421. 简化路径.cpp
80 lines (80 loc) · 1.98 KB
/
421. 简化路径.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Solution {
public:
stack<string> s;
/*
* @param path: the original path
* @return: the simplified path
*/
string simplifyPath(string &path) {
// write your code here
string str1="/", str2;
for(int i=1; i<path.size(); i++)
{
if(path[i] == '/')
{
if(str1 == "/" && str2.size()>0)
{
s.push(str1.append(str2));
str1 = "/";
str2 = "";
}
else if(str1 == "/..")
{
if(str2.size() == 0)
{
if(!s.empty())
s.pop();
}
else
{
s.push(str1.append(str2));
}
str1 = "/";
str2 = "";
}
else if(str1 == "/.")
{
if(str2.size() != 0)
{
s.push(str1.append(str2));
}
str1 = "/";
str2 = "";
}
}
else if(path[i] == '.')
{
str1.append(1, path[i]);
}
else
{
str2.append(1, path[i]);
}
}
str1 = str1.append(str2);
if(str1 == "/." || str1 == "/..")
{
s.push("/");
}
else if(str1 != "/")
{
s.push(str1);
}
string s1,s2;
if(s.empty())
{
s1 = "/";
}
else
{
while(!s.empty())
{
s2 = s.top();
s2.append(s1);
s1 = s2;
s.pop();
}
}
return s1;
}
};