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
相关推荐
rgb2gray29 分钟前
增强城市数据分析:多密度区域的自适应分区框架
大数据·python·机器学习·语言模型·数据挖掘·数据分析·llm
氵文大师1 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
程序员爱钓鱼1 小时前
用 Python 批量生成炫酷扫光 GIF 动效
后端·python·trae
封奚泽优1 小时前
下降算法(Python实现)
开发语言·python·算法
java1234_小锋1 小时前
基于Python深度学习的车辆车牌识别系统(PyTorch2卷积神经网络CNN+OpenCV4实现)视频教程 - 自定义字符图片数据集
python·深度学习·cnn·车牌识别
爱笑的眼睛111 小时前
深入理解MongoDB PyMongo API:从基础到高级实战
java·人工智能·python·ai
leoufung1 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
辣椒酱.1 小时前
jupyter相关
python·jupyter
郝学胜-神的一滴1 小时前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
火白学安全2 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全