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
相关推荐
rgeshfgreh2 分钟前
Python条件与循环实战指南
python
rgeshfgreh6 分钟前
通达信LC1文件结构解析指南
python
七夜zippoe20 分钟前
事件驱动架构:构建高并发松耦合系统的Python实战
开发语言·python·架构·eda·事件驱动
Kratzdisteln34 分钟前
【MVCD】PPT提纲汇总
经验分享·python
圣保罗的大教堂36 分钟前
leetcode 1161. 最大层内元素和 中等
leetcode
闲看云起38 分钟前
LeetCode-day6:接雨水
算法·leetcode·职场和发展
一个无名的炼丹师1 小时前
GraphRAG深度解析:从原理到实战,重塑RAG检索增强生成的未来
人工智能·python·rag
黛色正浓1 小时前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
用户8356290780512 小时前
用Python轻松管理Word页脚:批量处理与多节文档技巧
后端·python
进击的松鼠2 小时前
LangChain 实战 | 快速搭建 Python 开发环境
python·langchain·llm