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
相关推荐
Forever Nore12 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
北斗落凡尘13 小时前
LangGraph 入门实战(2)
python·langchain
ttod_qzstudio13 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
jufeng130714 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 1 篇】
人工智能·python·架构·agent
月光船幽幽15 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
_oP_i17 小时前
python 后缀 mjs文件
开发语言·python
北斗落凡尘17 小时前
如何使用LangGraph(1)
python·langchain
Livia要学习18 小时前
Python装饰器
开发语言·python
evans在进步19 小时前
LeetCode 200:岛屿数量——Java DFS 染色法详解
java·leetcode·深度优先
大模型丫丫19 小时前
LangChain4j:Java 生态的 AI 应用开发利器
java·人工智能·python