-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path71.cpp
119 lines (116 loc) · 3.29 KB
/
71.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
__________________________________________________________________________________________________
4ms
class Solution {
public:
string simplifyPath(string path) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int len = path.length();
vector<string>Path;
string now;
for(int i = 0; i < len; i++) {
if(path[i] == '/') continue;
if(path[i] == '.') {
if(i + 1 < len && path[i + 1] == '.' && (i + 2 >= len || path[i + 2] == '/')) {
if(!Path.empty()) {
// cout<<"POP\n";
Path.pop_back();
}
i++;
continue;
}else if(i + 1 >=len || path[i+1] == '/') {
continue;
}
}
now = "";
while(i < len && path[i] != '/') {
now += path[i]; i ++;
}
// cout<<now<<endl;
Path.push_back(now);
}
string ans = "/";
len = Path.size();
// cout<<"len: "<<len<<endl;
for(int i = 0; i < len - 1; i++) {
ans += Path[i];
ans += "/";
}
if(len) ans += Path[len - 1];
return ans;
}
};
__________________________________________________________________________________________________
9216 kb
static const auto speedup = []() {std::ios::sync_with_stdio(false); std::cin.tie(NULL); return 0; }();
class Solution {
public:
string simplifyPath(string path) {
string ret;
bool is_last_dir = false;
int period_len = 0; // .的长度
for (int i = 0; i < path.length(); i++)
{
if (path[i] == '.') {
bool tmp = isRelativePath(path, i, is_last_dir, period_len);
if (tmp) { //是相对路径
++i;
if (is_last_dir) { //表示上层目录
del_eol(ret);
ret.erase(ret.find_last_of('/') + 1);
++i;
}
}
else {
ret += path.substr(i, period_len);
i = i + period_len - 1;
}
continue;
}
else if (path[i] == '/') {
if (i + 1 < path.length() && path[i + 1] == '/') {
for (size_t j = i + 1; i < path.length(); j++){
if (path[j] != '/') { i = j - 1; break;}
}
}
if(ret.length() == 0 || ret[ret.length() - 1] != '/') ret += '/';
continue;
}
ret += path[i];
}
del_eol(ret);
return ret;
}
private:
//去掉字符串结尾的 '/'
void del_eol(string & ret) {
if (ret.length() > 1) {
int i = ret.length() - 1;
for (; i > 0; --i)
if (ret[i] != '/')
break;
ret.erase(i + 1);
}
}
//遇到.时,判断是否为 相对路径
//is_last_dir 表示是否为 上层目录(必须先判断方法的返回结果,再判断该值)
bool isRelativePath(string &path, int &i, bool &is_last_dir, int &period_len) {
period_len = 0;
is_last_dir = false;
for (int j = i; j < path.length(); j++, period_len++)
{
if (path[j] != '.') {
if (path[j] == '/' && period_len <= 2) {
is_last_dir = period_len == 2 ? true : false;
return true;
}
return false;
}
}
//这里表示的是.一直到结尾都是.
is_last_dir = period_len == 2 ? true : false;
return period_len <= 2;
}
};
__________________________________________________________________________________________________