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
相关推荐
Aawy12015 分钟前
Python生成器(Generator)与Yield关键字:惰性求值之美
jvm·数据库·python
沐硕36 分钟前
《基于改进协同过滤与多目标优化的健康饮食推荐系统设计与实现》
java·python·算法·fastapi·多目标优化·饮食推荐·改进协同过滤
乱世军军1 小时前
把 Python 3.13 降级到 3.11
开发语言·python
Y5neKO1 小时前
某国赛CTF逆向题目Writeup:re1
python·逆向·ctf
实心儿儿1 小时前
算法7:两个数组的交集
算法·leetcode·职场和发展
sheeta19981 小时前
LeetCode 每日一题笔记 日期:2025.03.19 题目:3212.统计X和Y频数相等的子矩阵数量
笔记·leetcode·矩阵
带娃的IT创业者1 小时前
WeClaw 架构演进史:从 0 到 1 构建跨平台 AI 助手的完整历程
人工智能·python·websocket·架构·fastapi·架构设计·实时通信
Storynone2 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
guts3502 小时前
使用python里的OpenCV包做简单的车道线检测
人工智能·python·opencv
sz-lcw2 小时前
HOG特征向量计算方法
人工智能·python·算法