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
相关推荐
全栈凯哥10 分钟前
27.Python datetime 与 time 完全指南
python
_日拱一卒17 分钟前
LeetCode:三数之和
算法·leetcode·排序算法
qiumingxun18 分钟前
Redis——使用 python 操作 redis 之从 hmse 迁移到 hset
数据库·redis·python
2401_8735449232 分钟前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
m0_5698814735 分钟前
进阶技巧与底层原理
jvm·数据库·python
Highcharts.js37 分钟前
Highcharts for Python|用 Pythonic 的方式构建AI数据可视化图表
前端·人工智能·python·信息可视化·数据科学·highcharts·ai可视化
m0_7269659839 分钟前
关于conda
开发语言·python·conda
xxjj998a40 分钟前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
大尚来也42 分钟前
Java 线程池深度解析:ThreadPoolExecutor 七大参数与核心原理
java·python·算法
卡尔特斯1 小时前
uv 精简使用教程
python·ai编程