-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
42 lines (37 loc) · 940 Bytes
/
Solution.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
class Solution {
public:
int lengthLongestPath(string input) {
int i = 0, n = input.size();
int ans = 0;
stack<int> stk;
while (i < n) {
int ident = 0;
for (; input[i] == '\t'; ++i) {
++ident;
}
int cur = 0;
bool isFile = false;
for (; i < n && input[i] != '\n'; ++i) {
++cur;
if (input[i] == '.') {
isFile = true;
}
}
++i;
// popd
while (!stk.empty() && stk.size() > ident) {
stk.pop();
}
if (stk.size() > 0) {
cur += stk.top() + 1;
}
// pushd
if (!isFile) {
stk.push(cur);
continue;
}
ans = max(ans, cur);
}
return ans;
}
};