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
相关推荐
reasonsummer2 分钟前
【教学类-134-01】20260414 Python制作童话故事音频
开发语言·python
Irene19913 分钟前
推荐学 Python 的编辑器:PyCharm(附:下载安装教程)
python·编辑器
wfbcg6 分钟前
每日算法练习:LeetCode 54. 螺旋矩阵 ✅
算法·leetcode·矩阵
郝学胜-神的一滴18 分钟前
Python 多线程编程从入门到精通:原理+实战+最佳实践
开发语言·网络·python·pycharm
郝学胜-神的一滴20 分钟前
深度学习激活函数核心精讲:Sigmoid 原理、推导与工程实践
人工智能·pytorch·python·深度学习·神经网络·机器学习
好家伙VCC20 分钟前
**TEE在嵌入式安全中的应用实践:基于ARM TrustZone的加密存储方案设计与实现*
java·arm开发·python·struts·安全
亚空间仓鼠25 分钟前
Python学习日志(二):基础语法
windows·python·学习
阿kun要赚马内26 分钟前
Python装饰器的原理详解
开发语言·python
2201_7568473328 分钟前
uni-app怎么接极光推送 uni-app消息推送App端接入【教程】
jvm·数据库·python
_日拱一卒30 分钟前
LeetCode:206反转链表
算法·leetcode·链表