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
相关推荐
夏鹏今天学习了吗2 小时前
【LeetCode热题100(95/100)】寻找重复数
算法·leetcode·职场和发展
火云洞红孩儿5 小时前
告别界面孤岛:PyMe如何用一站式流程重塑Python GUI开发?
开发语言·python
攻城狮7号5 小时前
不懂代码也能造?TRAE+GLM-4.6 手把手教你搭心理咨询智能客服小程序
python·小程序·uni-app·vue·trae·glm我的编程搭子·glm-4.6
叫我辉哥e15 小时前
新手进阶Python:办公看板集成ERP跨系统同步+自动备份+AI异常复盘
开发语言·人工智能·python
圣保罗的大教堂6 小时前
leetcode 3315. 构造最小位运算数组 II 中等
leetcode
布局呆星6 小时前
闭包与装饰器
开发语言·python
全栈测试笔记6 小时前
异步函数与异步生成器
linux·服务器·前端·数据库·python
木头左6 小时前
基于Backtrader框架的指数期权备兑策略实现与分析
python
Anastasiozzzz6 小时前
leetcode力扣hot100困难题--4.俩个正序数列的中位数
java·算法·leetcode·面试·职场和发展
素心如月桠7 小时前
cmd 输入 python --version 输出为空(windows11系统安装python后执行python --version没反应)
python