-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
42 lines (37 loc) · 1023 Bytes
/
Solution.java
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;
int n = input.length();
int ans = 0;
Deque<Integer> stack = new ArrayDeque<>();
while (i < n) {
int ident = 0;
for (; input.charAt(i) == '\t'; i++) {
ident++;
}
int cur = 0;
boolean isFile = false;
for (; i < n && input.charAt(i) != '\n'; i++) {
cur++;
if (input.charAt(i) == '.') {
isFile = true;
}
}
i++;
// popd
while (!stack.isEmpty() && stack.size() > ident) {
stack.pop();
}
if (stack.size() > 0) {
cur += stack.peek() + 1;
}
// pushd
if (!isFile) {
stack.push(cur);
continue;
}
ans = Math.max(ans, cur);
}
return ans;
}
}