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
相关推荐
2401_8796938719 分钟前
数据分析与科学计算
jvm·数据库·python
明月_清风42 分钟前
宿命的对决:深度对比 JavaScript 与 Python 的异步进化论
后端·python
明月_清风1 小时前
别再纠结 Conda 了!2026 年,uv 才是 Python 环境管理的唯一真神
后端·python
Thomas.Sir1 小时前
第一章:Python3 基础入门:从零基础到实战精通
python·ai
telllong1 小时前
BeeWare:Python原生移动应用开发
开发语言·python
tang777891 小时前
小红书平台用什么代理 IP 比较好?2026年3月实测数据 + 选型推荐
网络·爬虫·python·网络协议·tcp/ip·数据挖掘·ip
AlenTech2 小时前
141. 环形链表 - 力扣(LeetCode)
数据结构·leetcode·链表
dulu~dulu3 小时前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
编程之升级打怪3 小时前
用Python语言实现简单的Redis缓冲数据库驱动库
redis·python
佑白雪乐4 小时前
<ACM进度212题>[2026-3-1,2026-3-26]
算法·leetcode