码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Longest Absolute File Path

时间:2020-02-07 01:38:00      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:eve   for   while   str   pat   ret   ack   turn   add   

使用栈

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

  

[leetcode]Longest Absolute File Path

标签:eve   for   while   str   pat   ret   ack   turn   add   

原文地址:https://www.cnblogs.com/lautsie/p/12271474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!