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
相关推荐
武天旭1 小时前
AI安全--AI大模型的毒性偏见与安全对齐实践
人工智能·python·深度学习
圣保罗的大教堂2 小时前
leetcode 3736. 最小操作次数使数组元素相等 III 简单
leetcode
视觉小萌新2 小时前
AI Class 3 · Chapter 2-3:函数、异常、文件读写+JSON / JSONL 读写(小白版)
人工智能·python·json
文丑颜不良啊3 小时前
LeetCode1881~1990题目解析
java·leetcode
小小张说故事3 小时前
Python 的 obj.x 到底按什么顺序找?拆开数据描述符、实例字典和 __getattr__ 那条优先级链
python
用户11481867894844 小时前
水印的实现方式逻辑
前端·python
贾公子4 小时前
从单条内容到账号维度——AIGC 风险评分的工程实现
后端·python
文丑颜不良啊6 小时前
LeetCode1861~1880题目解析
java·leetcode
文丑颜不良啊6 小时前
LeetCode1921~1940题目解析
java·leetcode
木头左6 小时前
【Python】不点浏览器发博客:逆向 CSDN 创作中心接口做 md-publish 技能
python·自动化·api