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
相关推荐
z***565627 分钟前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
voidmort1 小时前
web3.py 简介:面向 Python 开发者的以太坊
开发语言·python·web3.py
后台开发者Ethan1 小时前
LangGraph 的持久化
python·langgraph
Swift社区1 小时前
LeetCode 427 - 建立四叉树
算法·leetcode·职场和发展
油炸小波1 小时前
02-AI应用开发平台Dify
人工智能·python·dify·coze
SunnyDays10112 小时前
从图片到PPT:用Python实现多图片格式(PNG/JPG/SVG)到幻灯片的批量转换
python·图片转ppt·png转ppt·jpg转ppt·svg转ppt·添加图片到ppt
CodeCraft Studio3 小时前
Excel处理控件Aspose.Cells教程:使用Python从Excel工作表中删除数据透视表
开发语言·python·excel·aspose·aspose.cells·数据透视表
普通网友3 小时前
用Python批量处理Excel和CSV文件
jvm·数据库·python
墨染点香3 小时前
LeetCode 刷题【160. 相交链表】
算法·leetcode·链表
少睡点觉3 小时前
LeetCode 238. 除自身以外数组的乘积 问题分析+解析
java·算法·leetcode