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
相关推荐
2601_9628857216 分钟前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
李航198324 分钟前
DeepSeekAI建模三 - 实战建模一组柜子
人工智能·python·计算机视觉·ai·ai编程
Nil2081 小时前
LeetCode 283移动零
数据结构·算法·leetcode
严谨的麻辣烫2 小时前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
我命由我123452 小时前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
FITA阿泽要努力2 小时前
Fork Agent:共享上下文的艺术
开发语言·python·agent
文丑颜不良啊2 小时前
LeetCode226~250题目解析
java·leetcode
DeeGLMath3 小时前
optimtool最新版研究梯度优化
python·optimtool·梯度优化
坐吃山猪3 小时前
Python-Hermite基函数
开发语言·python
空~自由3 小时前
Python 正则 . * ? .* .*? (.*?) 通俗讲解
开发语言·python