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
相关推荐
船长Talk1 分钟前
NumPy+Pandas数据分析基础完全指南
python
Tisfy2 分钟前
LeetCode 2515.到目标字符串的最短距离:从中间往两边遍历
算法·leetcode·字符串·题解·数组·遍历
Wyz201210242 分钟前
宝塔面板安装后显示无法连接数据库_检查MySQL服务状态
jvm·数据库·python
2301_777599373 分钟前
Redis如何优化大量对象存储_利用Hash结构减少内存碎片占用
jvm·数据库·python
2301_777599375 分钟前
Python怎么解压tar.gz_tarfile模块提取打包文件操作
jvm·数据库·python
小白学大数据7 分钟前
Python 爬取图片攻略:告别水印,批量保存高清图片
开发语言·python
2301_815279529 分钟前
HTML怎么标注密钥权限范围_HTML “仅读取用户信息”说明【操作】
jvm·数据库·python
m0_6784854510 分钟前
Go语言怎么用Jaeger_Go语言Jaeger链路追踪教程【实用】
jvm·数据库·python
NotFound48612 分钟前
c++如何通过解析二进制PE文件头检测程序是否开启了DEP保护机制【进阶】
jvm·数据库·python
zhangchaoxies15 分钟前
PHP源码能否在NAS设备上运行_NAS部署PHP源码可行性【教程】
jvm·数据库·python