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
相关推荐
高洁012 小时前
DNN案例一步步构建深层神经网络(二)
人工智能·python·深度学习·算法·机器学习
Insight.2 小时前
背包问题——01背包、完全背包、多重背包、分组背包(Python)
开发语言·python
Lucky高2 小时前
Pandas库实践1_预备知识准备
python·pandas
Salt_07282 小时前
DAY 36 官方文档的阅读
python·算法·机器学习·github
k***92163 小时前
Python 科学计算有哪些提高运算速度的技巧
开发语言·python
superman超哥3 小时前
仓颉条件变量深度解析与实践:解锁高效并发同步
开发语言·python·c#·仓颉
长空任鸟飞_阿康3 小时前
LangGraph 技术详解:基于图结构的 AI 工作流与多智能体编排框架
人工智能·python·langchain
love530love3 小时前
ComfyUI 升级 v0.4.0 踩坑记录:解决 TypeError: QM_Queue.task_done() 报错
人工智能·windows·python·comfyui
阿坤带你走近大数据4 小时前
Python基础知识-数据结构篇
开发语言·数据结构·python
小智RE0-走在路上4 小时前
Python学习笔记(7)--集合,字典,数据容器总结
笔记·python·学习