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
相关推荐
ZC跨境爬虫几秒前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
circuitsosk21 分钟前
从零搭建行业知识平台:向量库+图数据库+传统关系库的多模检索统一层设计
数据库·python·oracle·向量数据库·rag·多模检索
whcyhhh29 分钟前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
Navigator_Z35 分钟前
LeetCode //C - 1220. Count Vowels Permutation
c语言·算法·leetcode
AR-26710-38 分钟前
机器学习复习Day8——异常检测
人工智能·python·机器学习·scikit-learn
千里码aicood43 分钟前
Django 基于Django的电脑推荐与分析可视化系统设计与实现
python·django·电脑
未若君雅裁1 小时前
Agent 的结构化输出,ProviderStrategy、ToolStrategy 与错误重试
python·langchain
洛兮银儿1 小时前
pycharm选中同一个词的标记颜色的修改
ide·python·pycharm
feilieren1 小时前
leetcode - 389. 找不同
算法·leetcode
郝学胜_神的一滴1 小时前
Effective Python 条款 6:善用拆包告别恼人的下标索引
python·pycharm