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
相关推荐
H_unique15 分钟前
LangChain:调用工具Ⅲ
python·langchain
醉舞经阁半卷书11 小时前
深入掌握LangChain
python·langchain
CDN3601 小时前
[硬核] 你的DNS正在“裸奔”?用Python手撕DNS劫持与隧道检测逻辑
开发语言·网络·python
kybs19911 小时前
springboot视频推荐系统--附源码72953
java·spring boot·python·eclipse·asp.net·php·idea
BU摆烂会噶2 小时前
【LangGraph】运行时上下文(Runtime Context)
人工智能·python·langchain
xingbuxing_py3 小时前
精华贴分享|北交所:小市值策略的“甜蜜陷阱”还是“弹性引擎”?——一份轻度理解
python·金融·股票·理财·量化投资·股市·炒股
yj15583 小时前
在装修预算有限的情况下,哪些地方可以省?
python
TickDB3 小时前
Python 接入国内期货 Tick 行情:从 CTP 到统一 API 的工程实践
python·websocket
趣知岛3 小时前
2026最新Python零基础入门教程,从环境搭建到实战精通(附源码)
python·青少年编程
努力努力再努力FFF4 小时前
别再乱学PS、Python了,普通大学生该看懂的技能趋势
开发语言·python