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
相关推荐
科雷软件测试20 分钟前
Python中itertools.product:快速生成笛卡尔积
开发语言·python
派大星~课堂3 小时前
【力扣-142. 环形链表2 ✨】Python笔记
python·leetcode·链表
Thomas.Sir3 小时前
第一章:Agent智能体开发实战之【初步认识 LlamaIndex:从入门到实操】
人工智能·python·ai·检索增强·llama·llamaindex
ZTL-NPU4 小时前
Jetbrains开发ros
ide·python·pycharm·编辑器·ros·clion
会编程的土豆4 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式
环黄金线HHJX.4 小时前
TSE框架配置与部署详解
开发语言·python
前端摸鱼匠5 小时前
YOLOv11与OpenCV 联动实战:读取摄像头实时视频流并用 YOLOv11 进行检测(三)
人工智能·python·opencv·yolo·目标检测·计算机视觉·目标跟踪
Pyeako5 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 78. 子集 | C++ 回溯算法题解
c++·算法·leetcode
喝凉白开都长肉的大胖子6 小时前
在 Matplotlib 中fontweight一般怎么设置
python·matplotlib