leetcode388. Longest Absolute File Path

raledong發表於2019-02-18

題目要求

Suppose we abstract our file system by a string in the following manner:

The string "dir
	subdir1
	subdir2
		file.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
	subdir1
		file1.ext
		subsubdir1
	subdir2
		subsubdir2
			file2.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.

要求從String字串中找到最長的檔案路徑。這裡要注意,要求的是檔案路徑,資料夾路徑不予考慮。檔案和資料夾的區別在於檔案中一定包含.

這裡
代表根目錄平級,每多一個 就多一層路徑,這一層路徑都是相對於當前的上層路徑的。
dir
subdir1
subdir2
file.ext
為例
dir為第0層目錄

subdir1
代表subdir1是第一層目錄,且其是當前父目錄dir的子目錄

subdir2代表subdir2為第一層目錄,且其是當前父目錄dir的子目錄,此時的一級父目錄從subdir1更新為subdir2

file.ext
代表tfile.ext為二級目錄,位於當前一級目錄subdir2之下

思路和程式碼

綜上分析,我們可以記錄一個資訊,即當前每一級的目錄究竟是誰,每次只需要保留當前一級目錄已有的路徑長度即可。還是拿上面的例子dir
subdir1
subdir2
file.ext

遍歷完dir: 0級目錄長度為3
遍歷完
	subdir: 0級目錄長度為3, 1級目錄長度為11(dir/subdir1)
遍歷完
	subdir2: 0級目錄長度為3, 1級目錄長度為11(dir/subdir2)
遍歷完
		file.ext: 0級目錄長度為3, 1級目錄長度為11(dir/subdir2), 2級目錄長度為20

綜上,最長的檔案路徑長為20

程式碼如下:

    public int lengthLongestPath(String input) {
         if(input==null || input.isEmpty()) return 0;
         //記錄當前每一級路徑對應的路徑長度
         List<Integer> stack = new ArrayList<Integer>();
         int left = 0, right = 0;
         int max = 0;
         int curDepth = 0;
         //當前遍歷的是檔案還是資料夾
         boolean isFile = false;
         while(right < input.length()) {
             char c = input.charAt(right);
             if(c == `
` || c == `	`) {
                 //如果是檔案分割符的起點,則處理前面的字串
                 if(right-1>=0 && input.charAt(right-1)!=`
` && input.charAt(right-1)!=`	`) {
                     int length = right - left;
                     if(stack.isEmpty()) {
                         stack.add(length+1);
                     }else if(curDepth == 0){
                         stack.set(0, length+1);
                     }else{
                         int prev = stack.get(curDepth-1);
                         int now = prev + length + 1;
                         if(stack.size() <= curDepth) {
                             stack.add(now);
                         }else{
                             stack.set(curDepth, now);
                         }
                     }
                     if(isFile) {
                         max = Math.max(max, stack.get(curDepth)-1);
                     }
                     left = right;
                     isFile = false;
                 }
                 
                 //如果是檔案分隔符的末尾,則處理檔案分隔符,判斷是幾級路徑
                 if(right+1<input.length() && input.charAt(right+1)!=`
` && input.charAt(right+1) !=`	`){
                     curDepth = right - left;
                     left = right+1;
                 }
             }else if(c == `.`) {
                 isFile = true;
             }
             right++;
         }
         //處理最後的字串
         if(left != right && isFile) {
             if(curDepth == 0) {
                 max = Math.max(max, right - left);
             }else {
                 max = Math.max(max, stack.get(curDepth-1) + right - left);
             }
         }
         return max;
     }

相關文章