Leetcode算法比赛----Longest Absolute File Path
问题描述
Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.
Note:
The name of a file contains at least a . and an extension.
The name of a directory or sub-directory will not contain a ..
Time complexity required: O(n) where n is the size of the input string.
Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.
package code;
import java.util.ArrayList;
public class Solution {
static int startPos=0;
static int nextLevel=0;
public int lengthLongestPath(String input) {
startPos=0;
MyList list=new MyList(null, 0, 0, null);//新建一个空的MyList,表示根目录
deal(list,input, 0);
if(list.containsFile){
return list.subLength-1; //因为在第一层的目录之前算上了'/' 符号,要去除掉
}
else{
return 0;
}
}
public static void deal(MyList parent,String input,int curLevel){
while(startPos<input.length()){ //startPos是全局指针,用以标识当前正在处理原是字符串的哪一部分
String curStr=input.substring(startPos);
int len=curStr.indexOf('\n');
if(len>0){
startPos+=len+1;
String fileName=curStr.substring(0, len);
MyList current=new MyList(parent,len,0,fileName);
parent.subs.add(current);
//current.myLength=fileName.length();
current.myLength=len; //省去了fileName.length()函数调用
if(fileName.indexOf('.')>0){
parent.containsFile=true;
if(parent.subLength<len+1){
parent.subLength=len+1;//加 1 是为了在子目录之前算上'/'符号
}
}
nextLevel=findNextLevel(input);
startPos+=nextLevel;
if(nextLevel==curLevel+1){
//正好是下一级
deal(current,input,nextLevel);
if(current.containsFile){
parent.containsFile=true;
int newLength=current.myLength+current.subLength+1;//加 1 是为了在子目录之前算上'/'符号
if(parent.subLength<newLength){
parent.subLength=newLength;
}
}
}
if(nextLevel<curLevel){
/*说明接下来要处理的文件或目录名层级比本层级高,因此要退出当前层 级的处理,也正是因为下一个层级可能比
* 之前好几个层级都高,所以应当把nextLevel定义为全局变量,使得 退出一个层级的deal后,nextLevel得以完整,
* 在返回到栈中的上一个deal的环境中时,让会继续比较那个栈环境中 的currentLevel和nextLevel
*/
break;
}
//如果nextLevel==curLevel,那么就继续循环
}
else{
//len<0说明当前处理的是最后一个 file or directory 的name
startPos+=curStr.length();
MyList current=new MyList(parent,len,-1,curStr);//故意将 subLength=-1
parent.subs.add(current);
if(curStr.indexOf('.')>0){
//是 file
parent.containsFile=true;
if(parent.subLength<curStr.length()+1){
parent.subLength=curStr.length()+1;
}
}
break;
}
}
}
public static int findNextLevel(String input){
int level=0;
while(input.charAt(startPos+level)=='\t'){//通过数制表符的个数判断层级
level++;
}
return level;
}
static class MyList{;
String myName;
MyList parent;
int myLength;
int subLength;
boolean containsFile=false;//默认情况下子目录下没有File
ArrayList<MyList>subs=new ArrayList<>();
public MyList(MyList parent,int myLength,int subLength,String myName){
this.parent=parent;
this.myLength=myLength;
this.subLength=subLength;
this.myName=myName;
}
}
}
Leetcode算法比赛----Longest Absolute File Path的更多相关文章
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode: Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- POJ-1258 Agri-Net(最小生成树)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...
- Java之重载(Overload)与重写(Overwrite)总结
内容来源为:<孙卫琴面向对象编程>,本随笔简单总结,具体内容可参见概述第6章,写的挺清晰: 一. 重载(Overload) 1. 有时候类的同一种功能有多种实现方式,到底采用哪种实现方式, ...
- JVM-垃圾收集过程的内存管理
JDK1.7 JVM的垃圾收集算法有 1. 标记-清除算法: 2. 复制算法:在商业虚拟机都是使用这种算法来回收新生代的 3. 标记-整理算法: 4.分代收集算法: JDK1.7 JVM的垃圾收集器有 ...
- js跳出循环:break 、continue 、return
js跳出循环:break .continue .return 本文虽然讲的是js里跳出循环的方法,但是jquery其实就是在js的基础上封装而来的,所以一些js里的语法应用,在jquery里也是通用的 ...
- spring data 自定义接口
1 spring data jpa 虽然说spring data 提供了很多DAO 接口,但是依然可能不能满足我们日常的使用,所以,有时我们需要自定义接口方法.自定义接口方法步骤如下: 1. 创建自 ...
- latex排版(CTeX winEdit输出“系统找不到指定的文件”的终极解决办法)
WinEdit的菜单栏中:Options->Execution Modes 弹出的界面见下面的图: 看上图中“1”所示位置,后面3项出现了“?”号(出现“系统找不到指定的文件”的错误时,所有项后 ...
- FOCUS数据管理:数据字典与多维模型
这是我从工厂出来的第五个年头,也是我第一次主动写年终总结: 回想这一年来一点成就都没有,想想都有点后怕:而且每天还是干着同样的事情,容易被新手代替,由于我比较厌烦每天重复的干同样的活,所以我开始思考是 ...
- TCP/IP协议的三次握手及实现原理
TCP/IP是很多的不同的协议组成,实际上是一个协议组,TCP用户数据报表协议(也称作TCP传输控制协议,Transport Control Protocol.可靠的主机到主机层协议.这里要先强调一下 ...
- JAVA泛型——逆变
在上篇<JAVA泛型——协变>这篇文章中遗留以下问题——协变不能解决将子类型添加到父类型的泛型列表中.本篇将用逆变来解决这个问题. 实验准备 我们首先增加以下方法,见代码清单1所示. 代码 ...
- Codeforces 985G. Team Players
Description 有 \(n\) 个人 , \(m\) 对人有冲突 , 你要从这 \(n\) 个人中选出三个人成为一组 , 使得同一组的人不存在一对有冲突 题面 Solution 容斥 答案=总 ...