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
相关推荐
云和数据.ChenGuang8 分钟前
python大小整数范围解析
java·开发语言·python
cpolar技术支持23 分钟前
端口被谁占了、这台机上开了哪些监听?本机起一个端口占用只读清单,cpolar 给异地同事远程看谁在监听
运维·python·内网穿透·cpolar·端口占用
长坡夜行31 分钟前
没有运维的交易系统,是怎么自己活下去的
python
WeiXin_DZbishe40 分钟前
基于springboot装修风格选择系统的设计与开发-计算机毕设【课程设计】77964
java·vue.js·spring boot·后端·python·php·课程设计
XZ-0700011 小时前
week9-三元表达式
java·前端·python
峰哥AI手记1 小时前
金融合规问答助手-RAG查询路由落地
python·aigc
benchmark_cc1 小时前
量化项目启动时,应该先设计数据接口还是先编写策略?从系统架构角度做出正确选择
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
CaptainT1 小时前
第 4 卷:面向对象、数据建模与类型系统
python
2601_962885721 小时前
如何用 Python 判断一只股票“追涨”还是“抄底”更有效?
android·开发语言·python
用户7783366132111 小时前
选 SERP API 别靠感觉:用 SerpBase 实测的评估脚本与判定标准
python·api