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
相关推荐
shirsl15 小时前
算法 Day1-数组 / 哈希 + 双指针
python·算法·哈希算法
广州山泉婚姻21 小时前
Python序列号源码分析
python
计算机源码社21 小时前
【大数据项目实战】基于大数据的影视内容生态综合质量分析与可视化-基于数据挖掘的影视内容类型共现与口碑聚类分析系统
大数据·人工智能·python·数据挖掘·数据分析·毕业设计·课程设计
C^h21 小时前
pytorch 适合初学者 0基础学习
人工智能·pytorch·python
小柯南敲键盘21 小时前
跨马翻译:AI批量图片翻译工具,跨境电商视频字幕翻译与智能抠图一体搞定
人工智能·python·音视频
2601_962297251 天前
Python里behave和pytest-bdd哪个更适合中大型项目?为什么?
python·bdd·行为驱动开发·behave·pytest-bdd
不会就选b1 天前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode
YCOSA20251 天前
系统工具使用检测.exe (仅供娱乐)
python·microsoft
2601_962295331 天前
如何python实现网页的自动化
python·selenium·beautifulsoup·requests·网页自动化
ofoxcoding1 天前
GPT Image 2.5 API 实战:Python 调用实现图片生成与编辑
人工智能·python·gpt·ai