Python | Leetcode Python题解之第388题文件的最长绝对路径

题目:

题解:

python 复制代码
class Solution:
    def lengthLongestPath(self, input: str) -> int:
        ans, i, n = 0, 0, len(input)
        level = [0] * (n + 1)
        while i < n:
            # 检测当前文件的深度
            depth = 1
            while i < n and input[i] == '\t':
                depth += 1
                i += 1

            # 统计当前文件名的长度
            length, isFile = 0, False
            while i < n and input[i] != '\n':
                if input[i] == '.':
                    isFile = True
                length += 1
                i += 1
            i += 1  # 跳过换行符

            if depth > 1:
                length += level[depth - 1] + 1
            if isFile:
                ans = max(ans, length)
            else:
                level[depth] = length
        return ans
相关推荐
闲人编程20 分钟前
Python设计模式实战:用Pythonic的方式实现单例、工厂模式
开发语言·python·单例模式·设计模式·工厂模式·codecapsule·pythonic
py有趣2 小时前
LeetCode学习之实现strStr()
学习·算法·leetcode
lang201509282 小时前
Spring环境配置与属性管理完全指南
java·python·spring
夏鹏今天学习了吗2 小时前
【LeetCode热题100(52/100)】课程表
算法·leetcode·职场和发展
祁思妙想2 小时前
【LeetCode100】--- 101.重排链表【思维导图+复习回顾】
算法·leetcode·链表
Dream it possible!2 小时前
LeetCode 面试经典 150_链表_随机链表的复制(59_138_C++_中等)
c++·leetcode·链表
懒惰蜗牛2 小时前
Day10:Python实现Excel自动汇总
python·numpy·pandas·pip·1024程序员节·python读写excel
我是华为OD~HR~栗栗呀2 小时前
华为od-22届考研-C++面经
java·前端·c++·python·华为od·华为·面试
m0_748240252 小时前
华为OD机考:计算正方形数量(Python & C/C++ & JAVA & JS & GO)
c语言·python·华为od
我是华为OD~HR~栗栗呀2 小时前
华为OD, 测试面经
java·c++·python·华为od·华为·面试