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
相关推荐
花酒锄作田8 小时前
Postgres - Listen/Notify构建轻量级发布订阅系统
python·postgresql
Boop_wu8 小时前
[Java 算法] 字符串
linux·运维·服务器·数据结构·算法·leetcode
Thomas.Sir8 小时前
第二章:LlamaIndex 的基本概念
人工智能·python·ai·llama·llamaindex
m0_694845579 小时前
Dify部署教程:从AI原型到生产系统的一站式方案
服务器·人工智能·python·数据分析·开源
李昊哲小课10 小时前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
不知名的老吴10 小时前
返回None还是空集合?防御式编程的关键细节
开发语言·python
李昊哲小课11 小时前
Python办公自动化教程 - 第5章 图表创建 - 让数据可视化
python·信息可视化·数据分析·数据可视化·openpyxl
chushiyunen11 小时前
python pygame实现贪食蛇
开发语言·python·pygame
Dream of maid11 小时前
Python-基础2(流程控制)
python
Lenyiin12 小时前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python