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
相关推荐
充钱大佬19 小时前
Python测试基础教程
python·log4j·apache
初心丨哈士奇21 小时前
Python 四大基础容器|列表篇
python
明理的信封1 天前
AI 基础设施的“去 Python 化“:Rust 与 C# 的两条替代路径
人工智能·python·rust
麻雀飞吧1 天前
2026年AI量化开发,先跑通小流程再加复杂功能
人工智能·python
daphne odera�1 天前
PyCharm 中 Codex 插件启动失败:unknown variant default 的解决方法
python·chatgpt·pycharm
nbu04william1 天前
Deepseek-api省token的用法
python·大模型·token·deepseek
测试老哥1 天前
Pytest自动化测试详解
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
坚持学习前端日记1 天前
国产化适配全流程适配英伟达本地开发
人工智能·python
源图客1 天前
云途物流API开发-鉴权认证(Java)
java·网络·python
三川6981 天前
Tkinter库的学习记录06-变量类别
python