美文网首页
388. Longest Absolute File Path

388. Longest Absolute File Path

作者: Jeanz | 来源:发表于2017-09-01 09:37 被阅读0次

    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.

    一刷
    题解:
    利用stack, stack里面存当前directory的每个 parent directory的字符串长度。
    首先利用\n分割字符串。然后再利用函数st ring.lastIndexOf("\t"),即计算t的数目来判断string处于哪一层。
    例如"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext",当扫描到subdir1, stack的size是1,长度是"dir.lenth()+1",加1表示1
    如果当前的字符串中含有".", 表示它是文件,update一次最大的长度。

    class Solution {
        public int lengthLongestPath(String input) {
            Deque<Integer> stack = new ArrayDeque<>();
            stack.push(0); // "dummy" length
            int maxLen = 0;
            String[] tmp = input.split("\n");
            for(String s:tmp){
                int lev = s.lastIndexOf("\t")+1; // number of "\t"
                while(lev+1<stack.size()) stack.pop(); // find parent
                int len = stack.peek()+s.length()-lev+1; // remove "/t", add"/"at the end
                stack.push(len);
                // check if it is file
                if(s.contains(".")) maxLen = Math.max(maxLen, len-1); 
            }
            return maxLen;
        }
    }
    

    二刷
    注意,\t视为一个字符。
    用stack存parent dir的长度,例如dir/subdir1/
    lev+1<stack.size()是因为有dummy head 0

    String a = "\t\t\tabab";
    int b = a.lastIndexOf("\t");
    b = 2
    int len = a.length();
    len = 7;
    

    相关文章

      网友评论

          本文标题:388. Longest Absolute File Path

          本文链接:https://www.haomeiwen.com/subject/mklhjxtx.html