[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

  

posted @ 2020-02-06 23:12  阿牧遥  阅读(77)  评论(0编辑  收藏  举报