使用栈
class Solution: def lengthLongestPath(self, input: str) -> int: input = input + ' ' # add trailing dummy stack = [] level = 1 current = '' result = 0 for i in range(len(input)): if input[i] == ' ': 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] == ' ': level += 1 else: current += input[i] return result