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
相关推荐
喵手5 分钟前
Python爬虫零基础入门【第九章:实战项目教学·第5节】SQLite 入库实战:唯一键 + Upsert(幂等写入)!
爬虫·python·sqlite·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·sqlite入库实战
DN20209 分钟前
好用的机器人销售供应商
python
爬山算法10 分钟前
Hibernate(64)如何在Java EE中使用Hibernate?
python·java-ee·hibernate
lixin55655613 分钟前
基于迁移学习的图像分类增强器
java·人工智能·pytorch·python·深度学习·语言模型
翱翔的苍鹰1 小时前
多Agent智能体架构设计思路
人工智能·pytorch·python
小毅&Nora1 小时前
【后端】【Python】① Windows系统下Python环境变量设置指南
python·pip
Rabbit_QL8 小时前
【水印添加工具】从零设计一个工程级 Python 图片水印工具:WaterMask 架构与实现
开发语言·python
让我上个超影吧9 小时前
【力扣26&80】删除有序数组中的重复项
算法·leetcode
曲幽10 小时前
FastAPI多进程部署:定时任务重复执行?手把手教你用锁搞定
redis·python·fastapi·web·lock·works
漫随流水10 小时前
leetcode回溯算法(78.子集)
数据结构·算法·leetcode·回溯算法