153.Longest Absolute File Path
来源:互联网 发布:ubuntu命令行编辑文件 编辑:程序博客网 时间:2024/05/22 10:46
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
.
Subscribe to see which companies asked this question.
定义一个链表来存储当前的路径。
定义一个longest表示当前找到的最长的路径的长度。
链表中的元素为新建的一个内部类,该内部类有两个属性,level和value。分别存储该文件的路径层次(从0开始统计),value表示文件名。
Step1:首先把字符串以"\n"分割成字符数组;
Step2:如果后面没有元素了则结束,否则继续依次读后面的,读到之后首先生成一个新的element对象currentfile;
Step3:从链表后面开始倒序读,把level大于等于currentfile的节点都从链表中删除;
Step4:把currentfile结点加入到链表中;
Step5:如果currentfile是文件(如file.ext),则把链表中从头到尾即是该文件的路径,记录下该路径currentpath的长度;如果currentpath的长度比longest大,则更新longest;
Step6:转到Step2.
public int LengthLongestPath(string input) { if(input.Length == 0) { return 0; } ArrayList list = new ArrayList(); string[] arr = Regex.Split(input, "\n", RegexOptions.IgnoreCase); int len = arr.Length; /*Step1:首先把字符串以"\n"分割成字符数组;*/ Element current = new Element(arr[0]); // list.Add(current); int longest = 0; /*依次读后面的元素*/ for (int i = 0; i < len;i++ ) { current = new Element(arr[i]); /*从链表后面开始倒序读,把level大于等于currentfile的节点都从链表中删除;*/ for (int j = list.Count-1; j >= 0;j-- ) { Element e = (Element)list[j]; if (e.level >= current.level) { list.RemoveAt(j); } else { break; } } /*把currentfile结点加入到链表中;*/ list.Add(current); /*如果currentfile是文件(如file.ext),则把链表中从头到尾即是该文件的路径,记录下该路径currentpath;*/ if(current.value.Contains(".")){ StringBuilder temp = new StringBuilder(); Element node = (Element)list[0]; temp.Append(node.value); for (int k = 1; k < list.Count;k++ ) { node = (Element)list[k]; temp.Append("/"); temp.Append(node.value); } /*如果currentpath的长度比longestpath长,则更新longestpath;*/ if (temp.Length > longest) { longest = temp.Length; } } } return longest; } /** * 表示存到链表中的元素 */ class Element { /*str是类似于这种的\t\tsubsub,需要在这里计算出level和str*/ public Element(string str) { int index = str.LastIndexOf("\t"); level = index + 1;//计算level,比如\t\t\t\ts计算出来应该是4 value = str.Substring(index+1); } public int level;//存储该文件的路径层次(从0开始统计),value表示文件名 public string value;//value表示文件名 }
- 153.Longest Absolute File Path
- Longest Absolute File Path
- Longest Absolute File Path
- 388. Longest Absolute File Path
- leetcode:Longest Absolute File Path
- leetcode_388 Longest Absolute File Path
- 【Leetcode】Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- Leetcode Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- 【POJ 1852 ANTS】
- 腾讯视屏会员发布网站
- 装饰模式 Decorator
- 详解Java中的clone方法
- ubuntu12.04添加程序启动器到Dash Home
- 153.Longest Absolute File Path
- 整数几何函数库
- 301重定向
- jquery easyui window 不居中有关问题解决方案 (注意红色字体部分)
- 手机自适应设计代码以及案例
- 圆
- java实现二维码生成,使用Google zxing
- The servlets named [LogRecord] and [com.action.service.imp.LogServiceImp] are both mapped to the url
- ural 2065 - Different Sums