资讯专栏INFORMATION COLUMN

[LeetCode] 388. Longest Absolute File Path

wawor4827 / 620人阅读

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;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72001.html

相关文章

  • leetcode388. Longest Absolute File Path

    摘要:题目要求要求从字符串中找到最长的文件路径。这里要注意,要求的是文件路径,文件夹路径不予考虑。文件和文件夹的区别在于文件中一定包含。这里代表根目录平级,每多一个就多一层路径,这一层路径都是相对于当前的上层路径的。 题目要求 Suppose we abstract our file system by a string in the following manner: The strin...

    Dionysus_go 评论0 收藏0
  • [Leetcode] Binary Tree Longest Consecutive Sequenc

    摘要:递归法复杂度时间空间思路因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径有多长,二是目前路径的上一个节点的值。代码判断当前是否连续返回当前长度,左子树长度,和右子树长度中较大的那个 Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the lon...

    xi4oh4o 评论0 收藏0
  • leetcode 329. Longest Increasing Path in a Matrix

    摘要:题目要求思路和代码这里采用广度优先算法加上缓存的方式来实现。我们可以看到,以一个节点作为开始构成的最长路径长度是确定的。因此我们可以充分利用之前得到的结论来减少重复遍历的次数。 题目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

    heartFollower 评论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 评论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 评论0 收藏0

发表评论

0条评论

wawor4827

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<