当前位置:网站首页>力扣解法汇总388-文件的最长绝对路径
力扣解法汇总388-文件的最长绝对路径
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步做题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
假设有一个同时存储文件和目录的文件系统。下图展示了文件系统的一个示例:
这里将 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 <= 104
input 可能包含小写或大写的英文字母,一个换行符 '\n',一个制表符 '\t',一个点 '.',一个空格 ' ',和数字。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-absolute-file-path
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 首先使用\n分割,逐行统计。 * 使用一个栈来存储当前文件的父目录的路径长度。而当前文件或者文件夹的长度,就是父目录的路径长度+当前文件或文件夹的路径长度。 * 如果当前路径的文件\t的数量小于栈中的数量,则说明属于更上一层文件夹,则栈执行pop操作。 * 如果当前路径是一个文件,那么我们计算栈的最上层长度+当前文件路径长度,就可以算出来当前文件的路径总长度。 * 如果当前路径是文件夹,则我们计算当前文件夹路径长度后加入到栈中。
代码:
public class Solution388 {
public int lengthLongestPath(String input) {
String[] split = input.split("\n");
Stack<Integer> stack = new Stack<>();//
int maxLength = 0;
for (int i = 0; i < split.length; i++) {
String s = split[i];
int[] length = haveNum(s);
//判断包含几个/t
while (stack.size() > length[0]) {
stack.pop();
}
//这里+1是因为文件前面一定有前缀\的,所以要+1。而根节点是没有的
int oldLength = stack.size() == 0 ? 0 : stack.peek() + 1;
if (s.contains(".")) {
int currentLength = oldLength + length[1];
maxLength = Math.max(currentLength, maxLength);
} else {
stack.add(oldLength + length[1]);
}
}
return maxLength;
}
private int[] haveNum(String str) {
int num = 0;
for (int i = 1; i < str.length(); i++) {
if (str.startsWith("\t", i - 1)) {
num++;
continue;
}
break;
}
return new int[]{num, str.length() - num};
}
}边栏推荐
- 力扣解法汇总436-寻找右区间
- LeetCode Algorithm 1791. 找出星型图的中心节点
- php安全开发 12博客系统的 系统模块信息的修改
- 力扣解法汇总732-我的日程安排表 III
- RPA introduction
- 如何让杀毒软件停止屏蔽某个网页?以GDATA为例
- [adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
- Linux(CentOS6)安装MySQL5.5版本数据库
- MySQL table common operation mind map
- 力扣解法汇总面试题 01.05. 一次编辑
猜你喜欢

RPA introduction

Knowledge points of mall development

leetcode:6. Zigzag transformation

In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!

Ozzanation - système d'action basé sur sse

BaseDexClassLoader那些事

leetcodeSQL:612. Nearest distance on plane

How to stop anti-virus software from blocking a web page? Take gdata as an example
![[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology](/img/16/2f9a235995cdd54ac9b85a68a7afcb.jpg)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology

Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
随机推荐
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
Comprehensive quality of teaching resources in the second half of 2019 - subjective questions
serialization and deserialization
Don't miss it! Five large data visualization screens that HR must collect
Linux(CentOS6)安装MySQL5.5版本数据库
Software engineering - system flow chart
MySQL高级部分知识点
如何定位关键词使得广告精准投放。
MySQL advanced knowledge points
力扣解法汇总面试题 17.11-单词距离
Knowledge points of mall development
初探性能优化!从2个月到4小时的性能提升!
Metaverse × How will smart cities develop?
Is the bidding price fixed for each click?
Lua function
Linux(CentOS7)安装MySQL-5.7版本
力扣解法汇总-剑指 Offer II 114. 外星文字典
Design practice of rongyun Im on electron platform
MySQL表常用操作思维导图
php开发 博客系统的公告模块的建立和引入