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
相关推荐
两万五千个小时29 分钟前
构建mini Claude Code:02 - 把 Bash 拆成专用工具(read_file, write_file 等)
人工智能·python
紫陌涵光1 小时前
108.将有序数组转换为二叉搜索树
数据结构·算法·leetcode
iAkuya1 小时前
(leetcode)力扣100 75前K个高频元素(堆)
java·算法·leetcode
henry1010101 小时前
Ansible自动化运维全攻略(AI生成)
linux·运维·python·ansible·devops
weixin_440401692 小时前
Python数据分析(空值、重复值检测删除与设置)
开发语言·python·数据分析
家的尚尚签3 小时前
高定木作企业实践:案例分享与成果展示
大数据·人工智能·python
美好的事情能不能发生在我身上3 小时前
Leetcode热题100中的:哈希专题
算法·leetcode·哈希算法
haosend3 小时前
极简小白Python教程-实现能基本看懂和简单编写代码
python·路由器·交换机·网络自动化
逆境不可逃3 小时前
LeetCode 热题 100 之 41.缺失的第一个正数
算法·leetcode·职场和发展