成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 388. Longest Absolute File Path

wawor4827 / 642人閱讀

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 contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

The string "dirntsubdir1nttfile1.extnttsubsubdir1ntsubdir2nttsubsubdir2ntttfile2.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.

Solution
class Solution {
    public int lengthLongestPath(String input) {
        if (input == null || input.length() == 0) return 0;
        Deque stack = new ArrayDeque<>();
        stack.push(0);
        int max = 0;
        for (String str: input.split("
")) {
            int level = str.lastIndexOf("	")+1;
            while (level+1 < stack.size()) stack.pop();
            int len = stack.peek()+str.length()-level+1;
            stack.push(len);
            if (str.contains(".")) max = Math.max(max, len-1);

        }
        return max;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72001.html

相關(guān)文章

  • leetcode388. Longest Absolute File Path

    摘要:題目要求要求從字符串中找到最長(zhǎng)的文件路徑。這里要注意,要求的是文件路徑,文件夾路徑不予考慮。文件和文件夾的區(qū)別在于文件中一定包含。這里代表根目錄平級(jí),每多一個(gè)就多一層路徑,這一層路徑都是相對(duì)于當(dāng)前的上層路徑的。 題目要求 Suppose we abstract our file system by a string in the following manner: The strin...

    Dionysus_go 評(píng)論0 收藏0
  • [Leetcode] Binary Tree Longest Consecutive Sequenc

    摘要:遞歸法復(fù)雜度時(shí)間空間思路因?yàn)橐易铋L(zhǎng)的連續(xù)路徑,我們?cè)诒闅v樹的時(shí)候需要兩個(gè)信息,一是目前連起來的路徑有多長(zhǎng),二是目前路徑的上一個(gè)節(jié)點(diǎn)的值。代碼判斷當(dāng)前是否連續(xù)返回當(dāng)前長(zhǎng)度,左子樹長(zhǎng)度,和右子樹長(zhǎng)度中較大的那個(gè) Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the lon...

    xi4oh4o 評(píng)論0 收藏0
  • leetcode 329. Longest Increasing Path in a Matrix

    摘要:題目要求思路和代碼這里采用廣度優(yōu)先算法加上緩存的方式來實(shí)現(xiàn)。我們可以看到,以一個(gè)節(jié)點(diǎn)作為開始構(gòu)成的最長(zhǎng)路徑長(zhǎng)度是確定的。因此我們可以充分利用之前得到的結(jié)論來減少重復(fù)遍歷的次數(shù)。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

    heartFollower 評(píng)論0 收藏0
  • [LeetCode] 329. Longest Increasing Path in a Matri

    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...

    hss01248 評(píng)論0 收藏0
  • [LeetCode] 329. Longest Increasing Path in a Matri

    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...

    antz 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<