LeetCode 题解工作台
文件的最长绝对路径
假设有一个同时存储文件和目录的文件系统。下图展示了文件系统的一个示例: 这里将 dir 作为根目录中的唯一目录。 dir 包含两个子目录 subdir1 和 subdir2 。 subdir1 包含文件 file1.ext 和子目录 subsubdir1 ; subdir2 包含子目录 subsub…
3
题型
4
代码语言
3
相关题
当前训练重点
中等 · 栈·状态
答案摘要
class Solution: def lengthLongestPath(self, input: str) -> int:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路
题目描述
假设有一个同时存储文件和目录的文件系统。下图展示了文件系统的一个示例:

这里将 dir 作为根目录中的唯一目录。dir 包含两个子目录 subdir1 和 subdir2 。subdir1 包含文件 file1.ext 和子目录 subsubdir1;subdir2 包含子目录 subsubdir2,该子目录下包含文件 file2.ext 。
在文本格式中,如下所示(⟶表示制表符):
dir ⟶ subdir1 ⟶ ⟶ file1.ext ⟶ ⟶ subsubdir1 ⟶ subdir2 ⟶ ⟶ subsubdir2 ⟶ ⟶ ⟶ file2.ext
如果是代码表示,上面的文件系统可以写为 "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 。'\n' 和 '\t' 分别是换行符和制表符。
文件系统中的每个文件和文件夹都有一个唯一的 绝对路径 ,即必须打开才能到达文件/目录所在位置的目录顺序,所有路径用 '/' 连接。上面例子中,指向 file2.ext 的 绝对路径 是 "dir/subdir2/subsubdir2/file2.ext" 。每个目录名由字母、数字和/或空格组成,每个文件名遵循 name.extension 的格式,其中 name 和 extension由字母、数字和/或空格组成。
给定一个以上述格式表示文件系统的字符串 input ,返回文件系统中 指向 文件 的 最长绝对路径 的长度 。 如果系统中没有文件,返回 0。
示例 1:
输入:input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 输出:20 解释:只有一个文件,绝对路径为 "dir/subdir2/file.ext" ,路径长度 20
示例 2:
输入:input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 输出:32 解释:存在两个文件: "dir/subdir1/file1.ext" ,路径长度 21 "dir/subdir2/subsubdir2/file2.ext" ,路径长度 32 返回 32 ,因为这是最长的路径
示例 3:
输入:input = "a" 输出:0 解释:不存在任何文件
示例 4:
输入:input = "file1.txt\nfile2.txt\nlongfile.txt" 输出:12 解释:根目录下有 3 个文件。 因为根目录中任何东西的绝对路径只是名称本身,所以答案是 "longfile.txt" ,路径长度为 12
提示:
1 <= input.length <= 104input可能包含小写或大写的英文字母,一个换行符'\n',一个制表符'\t',一个点'.',一个空格' ',和数字。
解题思路
方法一
class Solution:
def lengthLongestPath(self, input: str) -> int:
i, n = 0, len(input)
ans = 0
stk = []
while i < n:
ident = 0
while input[i] == '\t':
ident += 1
i += 1
cur, isFile = 0, False
while i < n and input[i] != '\n':
cur += 1
if input[i] == '.':
isFile = True
i += 1
i += 1
# popd
while len(stk) > 0 and len(stk) > ident:
stk.pop()
if len(stk) > 0:
cur += stk[-1] + 1
# pushd
if not isFile:
stk.append(cur)
continue
ans = max(ans, cur)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) because each character of the input is processed once, with stack operations being O(1) per line. Space complexity is O(d) where d is the maximum directory depth, due to storing cumulative lengths in the stack. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Are you keeping track of path lengths dynamically without building the full directory tree?
- question_mark
How do you handle tabs to correctly identify directory depth in your stack?
- question_mark
Can you update the maximum path efficiently as you parse each file?
常见陷阱
外企场景- error
Miscounting depth by failing to handle multiple '\t' characters correctly.
- error
Forgetting to include '/' separators in cumulative path length calculations.
- error
Attempting recursion without stack optimization, causing O(n^2) behavior on deep inputs.
进阶变体
外企场景- arrow_right_alt
Return the actual longest absolute file path string instead of its length.
- arrow_right_alt
Count only files with a specific extension when computing the longest path.
- arrow_right_alt
Handle inputs where the filesystem string may contain empty lines or spaces at varying depths.