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
相关推荐
风送雨2 分钟前
八周Python强化计划(七)
开发语言·python
ππ很开心6665 分钟前
DAY 32 函数专题2:装饰器
开发语言·python
山沐与山18 分钟前
LangChain Tools解析:让Agent拥有超能力
人工智能·python·langchain
TonyLee01720 分钟前
python代码运行时间信息记录
python
曲幽26 分钟前
手把手搞定FastAPI静态文件:安全、上传与访问
css·python·fastapi·web·js·favicon·staticfiles
sandwu27 分钟前
AI Agent——可观测性链路集成&评测体系搭建(Langfuse)
人工智能·python·langchain·langfuse
未来之窗软件服务33 分钟前
幽冥大陆(八十四)Python 水果识别PTH 转 ONNX 脚本新 —东方仙盟练气期
人工智能·python·深度学习·仙盟创梦ide·东方仙盟·阿雪技术观
XLYcmy38 分钟前
高级密码生成器程序详解:专门设计用于生成基于用户个人信息的密码猜测组合
开发语言·数据结构·python·网络安全·数据安全·源代码·口令安全
阿豪只会阿巴1 小时前
【多喝热水系列】从零开始的ROS2之旅——Day4
c++·笔记·python·ros2
2401_841495641 小时前
【LeetCode刷题】寻找重复数
数据结构·python·算法·leetcode·链表·数组·重复数