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
相关推荐
Liue612312316 分钟前
基于MS-RCNN和X101-64x4d_FPN的船舶类型识别与检测方法研究
python
样例过了就是过了9 分钟前
LeetCode热题100 和为 K 的子数组
数据结构·算法·leetcode
Sunsets_Red27 分钟前
浅谈随机化与模拟退火
java·c语言·c++·python·算法·c#·信息学竞赛
Frostnova丶35 分钟前
LeetCode 67. 二进制求和
算法·leetcode
张3蜂1 小时前
Python pip 命令完全指南:从入门到精通
人工智能·python·pip
昌兵鼠鼠1 小时前
LeetCode Hot100 哈希
学习·算法·leetcode·哈希算法
人工智能AI酱1 小时前
【AI深究】高斯混合模型(GMM)全网最详细全流程详解与案例(附Python代码演示) | 混合模型概率密度函数、多元高斯分布概率密度函数、期望最大化(EM)算法 | 实际案例与流程 | 优、缺点分析
人工智能·python·算法·机器学习·分类·回归·聚类
脏脏a1 小时前
【优选算法・双指针】以 O (n) 复杂度重构数组操作:从暴力遍历到线性高效的范式跃迁
算法·leetcode·双指针·牛客·优选算法
kamisama_zhu1 小时前
LeetCode 热题100快速通关指南(附模板) (优化完整版,真人心得版,持续更新)
算法·leetcode·职场和发展
Faker66363aaa1 小时前
Faster-RCNN改进一基于R50-FPG的人脸与垃圾物体检测识别_crop640-50e_COCO
python