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
相关推荐
落羽凉笙33 分钟前
Python学习笔记(3)|数据类型、变量与运算符:夯实基础,从入门到避坑(附图解+代码)
笔记·python·学习
Quintus五等升38 分钟前
深度学习①|线性回归的实现
人工智能·python·深度学习·学习·机器学习·回归·线性回归
天远Date Lab43 分钟前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
哈里谢顿1 小时前
Python异常链:谁才是罪魁祸首?一探"The above exception"的时间顺序
python
AlenTech2 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
哈里谢顿2 小时前
验证 list() 会调用 `__len__` 方法的深度解析
python·django
vibag2 小时前
构建智能体与工具调用
python·语言模型·大模型·langgraph
sin_hielo2 小时前
leetcode 1161(BFS)
数据结构·算法·leetcode
小途软件2 小时前
高校宿舍访客预约管理平台开发
java·人工智能·pytorch·python·深度学习·语言模型
-dcr2 小时前
49.python自动化
运维·python·自动化