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
相关推荐
努力学算法的蒟蒻15 小时前
day41(12.22)——leetcode面试经典150
算法·leetcode·面试
ULTRA??15 小时前
基于range的函数式编程C++,python比较
c++·python·kotlin·c++20
小白学大数据15 小时前
Temu 商品历史价格趋势爬虫与分析
开发语言·javascript·爬虫·python
Amelia11111115 小时前
day32
python
Tisfy15 小时前
LeetCode 960.删列造序 III:动态规划(最长递增子序列)
算法·leetcode·动态规划·字符串·题解·逆向思维
多米Domi01116 小时前
0x3f第十天复习(考研日2)(9.18-12.30,14.00-15.00)
python·算法·leetcode
山海青风16 小时前
藏文TTS介绍:4 神经网络 TTS 的随机性与自然度
人工智能·python·神经网络·音视频
曲幽16 小时前
FastAPI入门:从简介到实战,对比Flask帮你选对框架
python·flask·fastapi·web·route·uv·uvicorn·docs
万邦科技Lafite16 小时前
淘宝开放API批量上架商品操作指南(2025年最新版)
开发语言·数据库·python·开放api·电商开放平台·淘宝开放平台
wheelmouse778816 小时前
Java工程师Python实战教程:通过MCP服务器掌握Python核心语法
java·服务器·python