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
相关推荐
别抢我的锅包肉6 分钟前
【FastAPI】 响应类型详解:从默认 JSON 到自定义响应
python·fastapi
智算菩萨16 分钟前
【Tkinter】15 样式与主题深度解析:ttk 主题系统、Style 对象与跨平台样式管理实战
开发语言·python·ui·ai编程·tkinter
样例过了就是过了34 分钟前
LeetCode热题100 柱状图中最大的矩形
数据结构·c++·算法·leetcode
weixin_4193497935 分钟前
Python 项目中生成 requirements.txt 文件
开发语言·python
wsoz41 分钟前
Leetcode哈希-day1
算法·leetcode·哈希算法
阿Y加油吧44 分钟前
LeetCode 二叉搜索树双神题通关!有序数组转平衡 BST + 验证 BST,小白递归一把梭
java·算法·leetcode
第一程序员1 小时前
Python与区块链:非科班转码者的指南
python·github
liu****1 小时前
LangChain-AI应用开发框架(六)
人工智能·python·langchain·大模型应用·本地部署大模型
witAI1 小时前
**AI仿真人剧制作2025推荐,专业团队与创新技术引领未来**
人工智能·python
♪-Interpretation2 小时前
第五节:Python的流程控制语句
python