摘要:題目要求要求從字符串中找到最長(zhǎng)的文件路徑。這里要注意,要求的是文件路徑,文件夾路徑不予考慮。文件和文件夾的區(qū)別在于文件中一定包含。這里代表根目錄平級(jí),每多一個(gè)就多一層路徑,這一層路徑都是相對(duì)于當(dāng)前的上層路徑的。
題目要求
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字符串中找到最長(zhǎng)的文件路徑。這里要注意,要求的是文件路徑,文件夾路徑不予考慮。文件和文件夾的區(qū)別在于文件中一定包含.。
這里
代表根目錄平級(jí),每多一個(gè) 就多一層路徑,這一層路徑都是相對(duì)于當(dāng)前的上層路徑的。
以dir
subdir1
subdir2
file.ext為例
dir為第0層目錄
subdir1代表subdir1是第一層目錄,且其是當(dāng)前父目錄dir的子目錄
subdir2代表subdir2為第一層目錄,且其是當(dāng)前父目錄dir的子目錄,此時(shí)的一級(jí)父目錄從subdir1更新為subdir2
file.ext代表tfile.ext為二級(jí)目錄,位于當(dāng)前一級(jí)目錄subdir2之下
綜上分析,我們可以記錄一個(gè)信息,即當(dāng)前每一級(jí)的目錄究竟是誰(shuí),每次只需要保留當(dāng)前一級(jí)目錄已有的路徑長(zhǎng)度即可。還是拿上面的例子dir subdir1 subdir2 file.ext:
遍歷完dir: 0級(jí)目錄長(zhǎng)度為3 遍歷完 subdir: 0級(jí)目錄長(zhǎng)度為3, 1級(jí)目錄長(zhǎng)度為11(dir/subdir1) 遍歷完 subdir2: 0級(jí)目錄長(zhǎng)度為3, 1級(jí)目錄長(zhǎng)度為11(dir/subdir2) 遍歷完 file.ext: 0級(jí)目錄長(zhǎng)度為3, 1級(jí)目錄長(zhǎng)度為11(dir/subdir2), 2級(jí)目錄長(zhǎng)度為20 綜上,最長(zhǎng)的文件路徑長(zhǎng)為20
代碼如下:
public int lengthLongestPath(String input) { if(input==null || input.isEmpty()) return 0; //記錄當(dāng)前每一級(jí)路徑對(duì)應(yīng)的路徑長(zhǎng)度 Liststack = new ArrayList (); int left = 0, right = 0; int max = 0; int curDepth = 0; //當(dāng)前遍歷的是文件還是文件夾 boolean isFile = false; while(right < input.length()) { char c = input.charAt(right); if(c == " " || c == " ") { //如果是文件分割符的起點(diǎn),則處理前面的字符串 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; } //如果是文件分隔符的末尾,則處理文件分隔符,判斷是幾級(jí)路徑 if(right+1
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/73302.html
Problem Suppose we abstract our file system by a string in the following manner: The string dirntsubdir1ntsubdir2nttfile.ext represents: dir subdir1 subdir2 file.ext The directory dir ...
摘要:遞歸法復(fù)雜度時(shí)間空間思路因?yàn)橐易铋L(zhǎng)的連續(xù)路徑,我們?cè)诒闅v樹(shù)的時(shí)候需要兩個(gè)信息,一是目前連起來(lái)的路徑有多長(zhǎng),二是目前路徑的上一個(gè)節(jié)點(diǎn)的值。代碼判斷當(dāng)前是否連續(xù)返回當(dāng)前長(zhǎng)度,左子樹(shù)長(zhǎng)度,和右子樹(shù)長(zhǎng)度中較大的那個(gè) Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the lon...
摘要:題目要求思路和代碼這里采用廣度優(yōu)先算法加上緩存的方式來(lái)實(shí)現(xiàn)。我們可以看到,以一個(gè)節(jié)點(diǎn)作為開(kāi)始構(gòu)成的最長(zhǎng)路徑長(zhǎng)度是確定的。因此我們可以充分利用之前得到的結(jié)論來(lái)減少重復(fù)遍歷的次數(shù)。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...
Problem Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move ou...
Problem Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move ou...
閱讀 3506·2021-11-24 11:17
閱讀 2293·2021-11-15 11:38
閱讀 3376·2021-10-14 09:42
閱讀 2949·2019-08-30 15:54
閱讀 2036·2019-08-28 18:09
閱讀 548·2019-08-26 11:48
閱讀 1639·2019-08-26 10:48
閱讀 2160·2019-08-26 10:45