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
相关推荐
◎菜澜子7 分钟前
pycharm从VCS获取项目报错unable to access:Recv failure:Connection was reset
ide·python·pycharm
Filotimo_9 分钟前
使用 Anaconda 环境在Jupyter和PyCharm 中进行开发
ide·经验分享·笔记·python·学习·jupyter·pycharm
黑金IT25 分钟前
WebSocket vs. Server-Sent Events:选择最适合你的实时数据流技术
网络·python·websocket·网络协议·fastapi
Amo Xiang26 分钟前
Python 常用模块(二):json模块
开发语言·python·json
cxylay27 分钟前
【python版】示波器输出的csv文件(时间与电压数据)如何转换为频率与幅值【方法③】
开发语言·python·示波器·频谱·csv文件·时域·频域
liangbm31 小时前
数学建模笔记—— 蒙特卡罗法
笔记·python·数学建模·概率论·概率统计·三门问题·蒙特卡罗法
叫我DPT1 小时前
Django——多apps目录情况下的app注册
python·django
ganjiee00071 小时前
力扣(leetcode)每日一题 2848 与车相交的点
java·算法·leetcode
q567315231 小时前
Metacritic 网站中的游戏开发者和类型信息爬取
数据库·python·线性代数·游戏·sqlite
__AtYou__2 小时前
Golang | Leetcode Golang题解之第406题根据身高重建队列
leetcode·golang·题解