[leetcode]Longest Absolute File Path
使用栈
class Solution:
def lengthLongestPath(self, input: str) -> int:
input = input + '\n' # add trailing dummy \n
stack = []
level = 1
current = ''
result = 0
for i in range(len(input)):
if input[i] == '\n':
while level <= len(stack):
stack.pop()
stack.append(current)
if '.' in current: # file
length = len('\\'.join(stack))
result = max(length, result)
current = ''
level = 1
elif input[i] == '\t':
level += 1
else:
current += input[i]
return result

浙公网安备 33010602011771号