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
相关推荐
老歌老听老掉牙6 分钟前
麻花钻切削过程的力学与效率解析
python·钻头·切削
happylifetree11 分钟前
Python27:核心语法-流程控制语句-match模式匹配
python
奋进的LY32 分钟前
spacy的安装和使用教程
人工智能·python·深度学习·github
znx93934 分钟前
机器学习赋能量化交易:解锁新时代投资的核心竞争优势
人工智能·python·机器学习·期魔方
xzal1239 分钟前
Python 列表操作笔记:append / extend / + / pop
笔记·python
minji...43 分钟前
LangGraph-AI智能体开发框架 - LangGraph 入门案例1 : 智能快递配送系统
人工智能·python·ai·langchain·大语言模型·agent·langgraph
Είναι η κοπέλα1 小时前
PyTorch 安装与验证
人工智能·pytorch·python
“AI国潮设计-小江”1 小时前
Python实战 | SDXL批量生成“潮汕英歌舞”国潮甜品IP,附核心Prompt与商用授权思路
开发语言·人工智能·python·prompt·aigc
Dream it possible!1 小时前
LeetCode_热题100_知识点总结
算法·leetcode·职场和发展
2601_962885721 小时前
如何用 Python 分析股票的日历效应?(星期效应与月份效应,附数据挖掘陷阱)
开发语言·python·数据挖掘