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
相关推荐
码云数智-园园10 分钟前
Python如何将律师的Excel噩梦变成自动化系统
python·自动化·excel
belldeep18 分钟前
python:Selenium 4.47 编码新的写法
python·selenium
卷无止境18 分钟前
除了开发api,FastAPI其实也可以配合jinja2模板写页面
后端·python·fastapi
OPEN-F22 分钟前
Python进阶教程:项目工程化与虚拟环境
开发语言·python
OPEN-F36 分钟前
Python进阶教程:自动化办公实战
python·c#·自动化
minglie143 分钟前
python串口的stream数据mock
python
晚风醉蝶1 小时前
1-16-计数排序-CountingSort
python·算法·排序算法
用户7783366132111 小时前
用搜索数据 API 做一个关键词联想组件(防抖 + 缓存 + 可复用)
python·api
AC赳赳老秦1 小时前
企业标签体系搭建:基于 OpenClaw 采集的多维度公开数据,构建企业画像标签库
java·运维·服务器·python·信息可视化·deepseek·openclaw
吃旺旺雪饼的小男孩1 小时前
U-Net 语义分割详解:原论文、PyTorch 实现与真实消融实
人工智能·pytorch·python·算法