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
相关推荐
先知后行。6 分钟前
python的类
开发语言·python
dyxal16 分钟前
Python包导入终极指南:子文件如何成功调用父目录模块
开发语言·python
nnerddboy18 分钟前
解决传统特征波段选择的不可解释性:2. SHAP和LIME
python·机器学习
电商API&Tina19 分钟前
【电商API接口】关于电商数据采集相关行业
java·python·oracle·django·sqlite·json·php
weixin_4215850138 分钟前
解释代码:val_pred = vxm_model.predict(val_input)--与tensor对比
python
xwill*43 分钟前
python 字符串拼接
linux·windows·python
superman超哥1 小时前
仓颉锁竞争优化深度解析
c语言·开发语言·c++·python·仓颉
一晌小贪欢1 小时前
【Python办公自动化】Python办公自动化常用库新手指南
开发语言·python·python自动化办公·python3·python办公自动化·python办公
其美杰布-富贵-李1 小时前
Python 反射完整学习笔记
笔记·python·学习·反射
热心市民小刘05051 小时前
12.23二叉树的层序遍历
python