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
相关推荐
knight_9___6 分钟前
RAG面试篇6
人工智能·python·机器学习·agent·rag
weixin_568996069 分钟前
如何用 IndexedDB 存储从 API 获取的超大列表并实现二级索引
jvm·数据库·python
2301_7751481517 分钟前
如何授权AWR报告生成_GRANT SELECT ANY DICTIONARY诊断权限
jvm·数据库·python
圣保罗的大教堂32 分钟前
leetcode 3761. 镜像对之间最小绝对距离 中等
leetcode
刀法如飞37 分钟前
一款Python语言Django框架DDD脚手架,开箱即用
python·架构·django
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 108. 将有序数组转换为二叉搜索树 | C++ 分治法详解
c++·算法·leetcode
itzixiao1 小时前
L1-051 打折(5分)[java][python]
java·python·算法
HappyAcmen2 小时前
10.常见报错排查与基础调试
开发语言·python
山川而川-R2 小时前
Windows新系统_安装anaconda-2026-4.24
python
ID_180079054732 小时前
Python 实现京东商品详情 API 数据准确性校验(极简可直接用)
java·前端·python