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
相关推荐
3DVisionary5 分钟前
装配检测丨蓝光三维扫描技术用于精密零部件3D检测与虚拟装配
python·3d·应变测量·金属3d打印·dic精度检验方法·各向异性·xtom蓝光三维扫描仪扫描
进击的小头9 分钟前
第7篇:动态规划的数值求解算法
python·算法·动态规划
喵手11 分钟前
Python爬虫实战:Playwright 监听快手直播间,自动化采集实时在线与礼物数据!
爬虫·python·爬虫实战·快手·playwright·零基础python爬虫教学·采集快手直播间数据
xsyaaaan5 小时前
leetcode-hot100-双指针:283移动零-11盛最多水的容器-15三数之和-42接雨水
算法·leetcode
一方热衷.7 小时前
YOLO26-Seg ONNXruntime C++/python推理
开发语言·c++·python
YMWM_8 小时前
如何将包路径添加到conda环境lerobot的python路径中呢?
人工智能·python·conda
田里的水稻9 小时前
ubuntu22.04_openclaw_ROS2
人工智能·python·机器人
梁正雄9 小时前
Python前端-2-css练习
前端·css·python
wefly20179 小时前
开发者效率神器!jsontop.cn一站式工具集,覆盖开发全流程高频需求
前端·后端·python·django·flask·前端开发工具·后端开发工具
Tisfy10 小时前
LeetCode 1888.使二进制字符串字符交替的最少反转次数:前缀和O(1)
算法·leetcode·字符串·题解