388. Longest Absolute File Path
来源:互联网 发布:简单c语言程序代码 编辑:程序博客网 时间:2024/06/07 00:32
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
.
思路:说实话,这个题做了TM一个下午,最开始想到的方法有
1. 递归
2. Stack
3. HashMap保存每一个depth
想来想去感觉1方法简单,于是就写吧
public class Solution { public int lengthLongestPath(String input) { int ret = 0; int start = input.indexOf('\n') + 2; if(start == 1)return input.indexOf('.')!=-1 ? input.length() : 0; String root = input.substring(0, input.indexOf('\n')); char[] cs = input.toCharArray(); StringBuilder sb = new StringBuilder(); for(int i=start; i<cs.length; i++) { if(cs[i] == '\n' && cs[i+1] == '\t' && cs[i+2] != '\t') { int sub = lengthLongestPath(sb.toString()); if(sub != 0)ret = Math.max(ret, 1 + root.length() + sub); sb = new StringBuilder(); start = i + 2; i += 2; } if(sb.length() != 0 || (cs[i]!='\n'&&cs[i]!='\t'))sb.append(cs[i]); } int sub = lengthLongestPath(sb.toString()); if(sub != 0)ret = Math.max(ret, 1 + root.length() + sub); return ret; }}
tune code好久,code也改的不成人样,仍然有bug,整个人都疯了(自己把思路转化成代码的能力还是比较差),后来发现一个判断depth的简单办法:先split by '\n',在求每个string的last index of '\t',这才可以用HashMap简洁的写出来
package l388;import java.util.HashMap;import java.util.Map;public class AC_Map { public int lengthLongestPath(String input) { Map<Integer, String> m = new HashMap<Integer, String>(); m.put(-1, ""); int max = 0; String[] ss = input.split("\n"); for(String s : ss) { int depth = s.lastIndexOf('\t') + 1; String path = m.get(depth-1) + s.substring(depth); m.put(depth, path); if(path.indexOf('.') != -1){ max = Math.max(max, path.length()+depth); } } return max; }}
- 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
- 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
- Longest Absolute File Path
- Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- leetcode -- 388. Longest Absolute File Path
- 388. [LeetCode]Longest Absolute File Path
- [Leetcode]388. Longest Absolute File Path
- 【LeetCode】 388. Longest Absolute File Path
- JAEE学习笔记(18)_jdbc篇
- 二维数组和二级指针
- C语言基础及指针②之指针内存分析
- 最小费用流算法不完全指南-2017华为软件精英挑战赛
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决
- 388. Longest Absolute File Path
- 使用POI读写word doc文件
- 微信小程序商品循环列表点击弹出当前的商品信息
- windows下通过wheel安装whl文件
- Page.OnPreLoad 方法对系统注入漏洞进行弥补
- 获取文件编码
- 子线程循环10次 主线程循环100次 然后如此反复50次
- 第六次C++实验
- Git检出项目--Idea中使用Git