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
相关推荐
触底反弹4 分钟前
面试被问到 Text2SQL,我用 DeepSeek 自己实现了一个
python·sqlite
Nil2084 分钟前
leetcode 78子集
数据结构·算法·leetcode
2601_9622955814 分钟前
python+selenium实现自动化测试
自动化测试·python·selenium·学习心得·web端测试
青 春 记 忆20 分钟前
零基础入门python66:FastAPI AI标题、摘要和标签
python·fastapi·后端开发
qq_225891746624 分钟前
基于Python+Django的LangGraph智能旅游规划系统
python·django·旅游
青 春 记 忆1 小时前
零基础入门python65:FastAPI 安全调用大模型API
python·fastapi·后端开发
维克兜率天2 小时前
【维克】特征归一化与标准化:为什么模型对数据的“尺度“很敏感?
人工智能·笔记·python·机器学习·量化
2601_962298672 小时前
Python multiprocessing PicklingError: Can&#39;t pickle &l
python·module·multiprocessing·function·picklingerror
CV山月2 小时前
大模型强化学习对齐:从 RLHF 框架到 PPO 算法原理
人工智能·python·大模型·强化学习·多模态·研究生
用户739548002062 小时前
本地 CSV 清洗的小细节:先标准化,再去重,并保留可检查的报告
python