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
相关推荐
nju_spy2 分钟前
动手学强化学习上交张伟楠(一)导论 + 多臂老虎机 MAB(ε-greedy+上置信界+汤普森采样)
人工智能·python·强化学习·actor-critic·多臂老虎机·汤普森采样·探索与利用
tjjucheng3 分钟前
专业做小程序定制开发的企业
python
ACERT3337 分钟前
6.吴恩达机器学习——TensorFlow与激活函数
人工智能·python·机器学习
APIshop18 分钟前
实战解析电商api:1688item_search-按关键字搜索商品数据
开发语言·python
就这个丶调调21 分钟前
Python学习路线全攻略:从入门到精通
人工智能·python·编程入门·学习路线
袁袁袁袁满22 分钟前
Python爬虫下载PDF文件
爬虫·python·pdf·python爬虫下载pdf文件
叫我:松哥24 分钟前
基于Flask开发的智能招聘平台,集成了AI匹配引擎、数据预测分析和可视化展示功能
人工智能·后端·python·信息可视化·自然语言处理·flask·推荐算法
程序员-King.28 分钟前
day126—二分查找—寻找旋转排序数组中的最小值(LeetCode-153)
算法·leetcode·二分查找
菜鸟233号28 分钟前
力扣494 目标和 java实现
java·数据结构·算法·leetcode