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
相关推荐
Data_agent1 小时前
学术爬虫实战:构建知网论文关键词共现网络的技术指南
python·算法
_一路向北_3 小时前
爬虫框架:Feapder使用心得
爬虫·python
皇族崛起3 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5
你想知道什么?3 小时前
Python基础篇(上) 学习笔记
笔记·python·学习
Swizard4 小时前
速度与激情:Android Python + CameraX 零拷贝实时推理指南
android·python·ai·移动开发
一直跑4 小时前
Liunx服务器centos7离线升级内核(Liunx服务器centos7.9离线/升级系统内核)
python
leocoder4 小时前
大模型基础概念入门 + 代码实战(实现一个多轮会话机器人)
前端·人工智能·python
Buxxxxxx4 小时前
DAY 37 深入理解SHAP图
python
ada7_4 小时前
LeetCode(python)108.将有序数组转换为二叉搜索树
数据结构·python·算法·leetcode
请一直在路上4 小时前
python文件打包成exe(虚拟环境打包,减少体积)
开发语言·python