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
相关推荐
weixin_4713830310 分钟前
17 Self-RAG —— 幻觉检测 + 答案质量评估
python·agent
叠层归一研究院17 分钟前
如何用程序搭建一个 AGI 种子系统(三):生长如何对接物理与数学宇宙
人工智能·python·算法·机器学习·transformer·agi
jufeng130728 分钟前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 5 篇】
python·ai agent·上下文压缩
AndrewHZ29 分钟前
图像处理入门008 | 阶段总结:环境测试与基础概念测验
图像处理·python·opencv·计算机视觉·cv
北斗落凡尘38 分钟前
LangGraph 入门实战(10)--时光回溯
python·langchain
今天AI了吗38 分钟前
Agent & AI 名词大扫盲
数据库·人工智能·python·sql·rust
张张123y1 小时前
Hermes Agent 是什么?它能做什么,以及如何部署到飞书
人工智能·python·飞书
SamChan901 小时前
用Playwright端到端测试PDF翻译功能:Web自动化测试实战
前端·python·ai·pdf·wpf
袁袁袁袁满1 小时前
Python爬虫实战:利用巨量IP获取跨境电商数据
爬虫·python·网络爬虫·爬虫实战·跨境电商·电商爬虫
jufeng13071 小时前
【系列:TDengine 工业物联网实战:从零搭起可运行系统 · 第 4 篇】
python·时序数据库·tdengine·asyncio