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
相关推荐
weixin_580614009 分钟前
如何在 Go 中使用 gocql 执行本地 CQL 脚本文件
jvm·数据库·python
weixin_5806140013 分钟前
mysql权限表查询性能如何优化_MySQL系统权限缓存原理
jvm·数据库·python
Irene199117 分钟前
Python 中常用内置函数分类总结(常用场景速查)
python
解救女汉子19 分钟前
mysql如何实现数据库降序输出_使用order by字段desc语句
jvm·数据库·python
北顾笙98027 分钟前
day26-数据结构力扣
数据结构·算法·leetcode
2402_8548083732 分钟前
c++怎么利用std--span在不拷贝的情况下解析大规模文件映射【进阶】
jvm·数据库·python
2301_7775993735 分钟前
Redis怎样管理16384个哈希槽_利用cluster-config-file持久化保存节点与槽位的映射关系
jvm·数据库·python
qq_3422958238 分钟前
Go语言怎么用GitHub Actions_Go语言GitHub Actions教程【基础】.txt
jvm·数据库·python
Wyz2012102439 分钟前
如何利用虚拟 DOM 实现无痕刷新?基于 VNode 对比的状态保持技巧
jvm·数据库·python
2501_9142459341 分钟前
AWS CodeBuild 中 PHP 8.0 运行时版本不支持的解决方案
jvm·数据库·python