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
相关推荐
Shaoshing43 分钟前
LeetCode100 32.最长有效括号
java·数据结构·leetcode
weixin_307779131 小时前
Jupyter SQL 表血缘分析并导出 Excel
开发语言·python·sql·算法·excel
benchmark_cc1 小时前
如何用 Python 计算盈亏痛苦比(Gain-to-Pain Ratio)?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
sbjdhjd1 小时前
后端编程语言 PHP 笔记(第 1–3 章)|PHP 简介、版本与运行环境安装
开发语言·人工智能·笔记·后端·python·ai·php
Ivanqhz1 小时前
wb 自动化搬运矩阵: 一条定型 “芯片选型偏好” 的话术?!
人工智能·python·机器学习
小白的高手之路1 小时前
【LeetCode 热题 100】240. 搜索二维矩阵 II|矩阵、二分查找、Z 字形查找(Python)
python·算法·leetcode·矩阵·力扣
geovindu1 小时前
python:DBSCAN
开发语言·后端·python·算法
劲夫学编程1 小时前
leetcode:最长连续序列
leetcode·最长连续序列
斯内普吖1 小时前
(开源)智慧物业管理系统实战指南 基于 Python + Django + uni-app + Vue + MySQL
vue.js·python·django·uni-app·开源
Ponp_8 小时前
用 StyleGAN2 做图像数据扩充实战
python·深度学习·生成对抗网络