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
相关推荐
米粒12 小时前
力扣算法刷题 Day 27
算法·leetcode·职场和发展
IAUTOMOBILE2 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
Mr_Xuhhh3 小时前
LeetCode hot 100(C++版本)(上)
c++·leetcode·哈希算法
TT_44194 小时前
python程序实现图片截图溯源功能
开发语言·python
小陈的进阶之路4 小时前
logging 日志模块笔记
python
cqbelt4 小时前
Python 并发编程实战学习笔记
笔记·python·学习
穿条秋裤到处跑4 小时前
每日一道leetcode(2026.03.31):字典序最小的生成字符串
算法·leetcode
智算菩萨5 小时前
【论文复现】Applied Intelligence 2025:Auto-PU正例无标签学习的自动化实现与GPT-5.4辅助编程实战
论文阅读·python·gpt·学习·自动化·复现
小陈工5 小时前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
老李的勺子6 小时前
Agent 记忆失效的 5 种方式:完整排查复盘
python·llm