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 subdir2subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1subdir2contains 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;    }}


原创粉丝点击