my solution using python ugly.


  • 0
    M

    ···
    class Solution(object):
    def lengthLongestPath(self, input):
    """
    :type input: str
    :rtype: int
    """
    pre_dir = -1
    cur_dir = 0
    max_length = 0
    pre_lengths = [0]
    cur_length = 0
    dirs = input.split('\n')

        for dir_str in dirs:
            cur_dir = dir_str.count('\t')
            if cur_dir > pre_dir:
                if len(dir_str[cur_dir:])+pre_lengths[cur_dir] > max_length and dir_str.find('.') != -1:
                    max_length = len(dir_str[cur_dir:])+pre_lengths[cur_dir]
                pre_lengths.append(pre_lengths[cur_dir]+len(dir_str[cur_dir:])+1)
                pre_dir = cur_dir
            elif cur_dir == pre_dir:
                if len(dir_str[cur_dir:])+pre_lengths[cur_dir] > max_length and dir_str.find('.') != -1:
                    max_length = len(dir_str[cur_dir:])+pre_lengths[cur_dir]
                pre_lengths[-1] = pre_lengths[cur_dir]+len(dir_str[cur_dir:])+1
            else:
                if len(dir_str[cur_dir:])+pre_lengths[cur_dir] > max_length and dir_str.find('.') != -1:
                    max_length = len(dir_str[cur_dir:])+pre_lengths[cur_dir]
                pre_lengths[cur_dir+1] = pre_lengths[cur_dir]+len(dir_str[cur_dir:])+1
    
        if not max_length:
            return max_length
        return max_length
    

    ···


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.