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
相关推荐
努力学算法的蒟蒻几秒前
day114(3.16)——leetcode面试经典150
算法·leetcode·职场和发展
魔道不误砍柴功1 分钟前
Java Function 高级使用技巧:从工程实战中来
java·开发语言·python
xixihaha13246 分钟前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
超越自我肖8 分钟前
python--while循环的基础案例
python
2501_921649498 分钟前
免费港股实时行情 API:功能、性能与接入指南
开发语言·后端·python·金融·restful
zh路西法9 分钟前
【宇树机器人强化学习】(四):Go2基础训练以及参数调节与解析
python·深度学习·ubuntu·机器学习·机器人
码路飞13 分钟前
315 曝光 AI 投毒产业链,我写了个 Python 脚本检测 AI 回答有没有「中毒」
python·aigc
q_354888515317 分钟前
计算机毕业设计源码:锦江酒店大数据分析与个性化推荐系统 Django框架 Vue 可视化 Hadoop 爬虫 协同过滤推荐算法 民宿 客栈(建议收藏)✅
python·机器学习·信息可视化·数据分析·django·课程设计·旅游
一叶落43819 分钟前
LeetCode 11:盛最多水的容器(C语言实现)
c语言·数据结构·算法·leetcode
_日拱一卒23 分钟前
LeetCode(力扣):二叉树的后序遍历
算法·leetcode·职场和发展