-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
47 lines (40 loc) · 852 Bytes
/
index.ts
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
/**
* 栈
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
* @param input
* @returns
*/
export function lengthLongestPath(input: string): number {
const len = input.length
let pos = 0
let ans = 0
const stack: number[] = []
while (pos < len) {
// 检测当前文件的深度
let depth = 1
while (pos < len && input[pos] === '\t') {
pos++
depth++
}
// 统计当前文件名称的长度
let isFile = false
let l = 0
while (pos < len && input[pos] !== '\n') {
if (input[pos] === '.')
isFile = true
l++
pos++
}
// 跳过当前的换行符
pos++
while (stack.length >= depth)
stack.pop()
if (stack.length)
l += stack[stack.length - 1] + 1
if (isFile)
ans = Math.max(ans, l)
else
stack.push(l)
}
return ans
}