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
相关推荐
Python私教3 分钟前
字节跳动开源图标库:2000+图标一键换肤的魔法
python·开源
何双新5 分钟前
第2讲、Odoo深度介绍:开源ERP的领先者
python·开源
2301_8050545638 分钟前
Python训练营打卡Day46(2025.6.6)
开发语言·python
曹勖之1 小时前
撰写脚本,通过发布/joint_states话题改变机器人在Rviz中的关节角度
开发语言·python·机器人·ros2
蹦蹦跳跳真可爱5891 小时前
Python----目标检测(训练YOLOV8网络)
人工智能·python·yolo·目标检测
梓仁沐白1 小时前
【Kotlin】注解&反射&扩展
开发语言·python·kotlin
緈福的街口1 小时前
【leetcode】3. 无重复字符的最长子串
算法·leetcode·职场和发展
Xi_Xu1 小时前
全面解析:tzst 归档格式的先进性与跨平台文件管理指南
python·pypy
Kusunoki_D1 小时前
Python-正则表达式(re 模块)
python·正则表达式
2401_863820891 小时前
Python 训练营打卡 Day 45
python