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
相关推荐
xufengzhu3 分钟前
Python库PyMySQL的使用指南
开发语言·python·pip
世辰辰辰8 小时前
批量修改图片/文本名子
开发语言·python·批量修改文件名
myenjoy_110 小时前
MQTT 与 Sparkplug B——从车间到云端的最后一公里
网络·python
颜酱11 小时前
LangChain 输出解析器:把模型回复变成你要的数据
python·langchain
2401_8734794011 小时前
企业安全运营中,如何用IP离线库提前发现失陷主机?三步实现风险画像
网络·数据库·python·tcp/ip·ip
weixin_5231853212 小时前
Java基础知识总结(四):引用数据类型与参数传递机制
java·开发语言·python
sheeta199812 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油12 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
码农飞哥12 小时前
我把RAG召回率从60%提到90%,就改了这两件事
python·知识库·向量检索·rag·效果提示
宸津-代码粉碎机12 小时前
Spring AI企业级实战|从RAG优化到Agent多工具调度
java·大数据·人工智能·后端·python·spring