-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path388.py
More file actions
29 lines (27 loc) · 1012 Bytes
/
388.py
File metadata and controls
29 lines (27 loc) · 1012 Bytes
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
# 56ms 14.9MB
class Solution:
def lengthLongestPath(self, input: str) -> int:
filesys = input.split('\n')
length_level = {}
maxlen = 0
for i in filesys:
level = i.count('\t')
length_level[level] = len(i) - level + 1
if level == 0:
length_level[level] -= 1
if '.' in i: # 是文件
maxlen = max(sum(list(length_level.values())[:level+1]), maxlen)
return maxlen
# 32ms 15MB
class Solution:
def lengthLongestPath(self, input: str) -> int:
filesys = input.split('\n')
length_level = {-1: 0}
maxlen = 0
for i in filesys:
level = i.count('\t')
length_level[level] = length_level[level-1] + len(i) - level
if '.' in i: # 是文件
maxlen = max(length_level[level] + level, maxlen) # 补'/'
return maxlen
# 另外注意,如果不用split的话,\n\t其实都是一个字符。。