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
相关推荐
0+1114 分钟前
算法 --栈
算法·leetcode
圣保罗的大教堂15 分钟前
leetcode 3723. 数位平方和的最大值 中等
leetcode
追光者4941 分钟前
我把 55 篇自动化日志翻了一遍:最容易出事的是这 4 个地方
python·自动化运维
2601_9628857242 分钟前
如何用 Python计算 CR能量指标(中间意愿指标)?
开发语言·python
打工仔折腾 AI44 分钟前
LangChain 工具调用实战:从 Tool 定义到 Agent 执行链路拆解
java·前端·人工智能·后端·python·langchain·ai agent 实战
hhb_6181 小时前
Flask+SQLite实现用户管理接口
python·sqlite·flask
全栈练习生1 小时前
RAG 准确率保障的体系化方案
python·ai
秋秋小事1 小时前
python 基础知识总结(一)
python
蜗牛互联网1 小时前
Python调用Gemini Structured Outputs实现工单路由门禁
java·开发语言·人工智能·python
TomEval1 小时前
【测AI】第12篇:NLP / CV / 语音 —— 三大 AI 方向速通
人工智能·python·自然语言处理·aigc·测试用例