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
相关推荐
DN202020 分钟前
当AI开始评估客户的“成交指数”
数据结构·人工智能·python·microsoft·链表
小小张说故事22 分钟前
Python图像处理利器:Pillow (PIL)入门指南
后端·python·图像识别
好家伙VCC35 分钟前
**标题:发散创新|用Python构建GAN图像生成器:从理论到实战全流程解析**---在深度学习飞速发展的今天,**生成对抗
java·python·深度学习·生成对抗网络
苏荷水43 分钟前
万字总结LeetCode100(持续更新...)
java·算法·leetcode·职场和发展
leikooo1 小时前
基于 GitHub Actions 的 Notion RSS 自动化部署指南
python·github·rss
l1t1 小时前
在python 3.14 容器中安装和使用chdb包
开发语言·python·clickhouse·chdb
yuanmenghao2 小时前
Linux 性能实战 | 第 17 篇:strace 系统调用分析与性能调优 [特殊字符]
linux·python·性能优化
bst@微胖子2 小时前
PyTorch深度学习框架项目合集一
人工智能·pytorch·python
Boxsc_midnight2 小时前
【vLLM服务器并发能力测试程序】写一个python小程序来进行并发测试
服务器·python·vllm