使用java找到最长的绝对文件路径。

时间:2021-05-17 08:58:02

I implemented the following method to find a longest absolute file path.

我实现了以下方法来找到最长的绝对文件路径。

 public static int lengthLongestPath(String input) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    if (input.length() == 0) return 0;
    int maxLength = 0;
    int subStringLength = 0;
    int previousLevel = 0;
    String[] paths = input.split("\n");
    for (String path : paths) {
        String[] substr = path.split("\t");
        String dirOrFile = substr[substr.length-1];
        int level = substr.length -1;

        if(level <= previousLevel && level !=0){
            previousLevel = level-1;
            subStringLength = map.get(previousLevel);
        }else if(level ==0){
            subStringLength = 0;
        }else{
            previousLevel = level;
        }
        subStringLength += dirOrFile.length();
        if(dirOrFile.contains(".")){
            maxLength = Math.max(subStringLength+level, maxLength);
        }
        map.put(level, subStringLength);
    }
    return maxLength;
}

However, it is failing for the following input. The expected output is 528. But my output is 549.

但是,它不能用于以下输入。预期的输出是528。但我的输出是549。

"sladjf\n\tlkjlkv\n\t\tlkjlakjlert\n\t\t\tlaskjglaksjf\n\t\t\t\tlakjgfljrtlj\n\t\t\t\t\tlskajflakjsvlj\n\t\t\t\t\t\tlskgjflkjrtlrjt\n\t\t\t\t\t\t\tlkjglkjlvkjdlvkj\n\t\t\t\t\t\t\t\tlfjkglkjfljdlv\n\t\t\t\t\t\t\t\t\tlkdfjglerjtkrjkljsd.lkvjlkajlfk\n\t\t\t\t\t\t\tlskfjlksjljslvjxjlvkzjljajoiwjejlskjslfj.slkjflskjldfkjoietruioskljfkljf\n\t\t\t\t\tlkasjfljsaljlxkcjzljvl.asljlksaj\n\t\t\t\tasldjflksajf\n\t\t\t\talskjflkasjlvkja\n\t\t\t\twioeuoiwutrljsgfjlskfg\n\t\t\t\taslkjvlksjvlkjsflgj\n\t\t\t\t\tlkvnlksfgk.salfkjaslfjskljfv\n\t\t\tlksdjflsajlkfj\n\t\t\tlasjflaskjlk\n\t\tlsakjflkasjfkljas\n\t\tlskjvljvlkjlsjfkgljfg\n\tsaljkglksajvlkjvkljlkjvksdj\n\tlsakjglksajkvjlkjdklvj\n\tlskjflksjglkdjbkljdbkjslkj\n\t\tlkjglkfjkljsdflj\n\t\t\tlskjfglkjdfgkljsdflj\n\t\t\t\tlkfjglksdjlkjbsdlkjbk\n\t\t\t\t\tlkfgjlejrtljkljsdflgjl\n\t\t\t\t\tsalgkfjlksfjgkljsgfjl\n\t\t\t\t\tsalkflajwoieu\n\t\t\t\t\t\tlaskjfglsjfgljkkvjsdlkjbklds\n\t\t\t\t\t\t\tlasjglriotuojgkjsldfgjsklfgjl\n\t\t\t\t\t\t\t\tlkajglkjskljsdljblkdfjblfjlbjs\n\t\t\t\t\t\t\t\t\tlkajgljroituksfglkjslkjgoi\n\t\t\t\t\t\t\t\t\t\tlkjglkjkljkljdkbljsdfljgklfdj\n\t\t\t\t\t\t\t\t\t\t\tlkjlgkjljgslkdkldjblkj\n\t\t\t\t\t\t\t\t\t\t\t\tlkjfglkjlkjbsdklj.slgfjalksjglkfjglf\n\t\t\t\t\t\t\t\t\t\t\t\tlkasjrlkjwlrjljsl\n\t\t\t\t\t\t\t\t\t\t\t\t\tlksjgflkjfklgjljbljls\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkjsglkjlkjfkljdklbjkldf\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkjglkjdlsfjdglsdjgjlxljjlrjsgjsjlk\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkjsgkllksjfgjljdslfkjlkasjdflkjxcljvlkjsgkljsfg\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlaskjlkjsakljglsdjfgksdjlkgjdlskjb\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkajsgfljfklgjlkdjgfklsdjklj\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkjfglkjlkgjlkjl.aslkjflasjlajglkjaf\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlkjasflgjlskjglkfjgklgsdjflkbjsdklfjskldfjgklsfdjgklfdjgl\n\tlskadjlkjsldwwwwwfj\n\t\tlkjflkasjlfjlkjajslfkjlasjkdlfjlaskjalvwwwwwwwwwwwwwwwkjlsjfglkjalsjgflkjaljlkdsjslbjsljksldjlsjdlkjljvblkjlkajfljgasljfkajgfljfjgldjblkjsdljgsldjg.skljf"

Can someone help me find the issue with my code.

有人能帮我找到我的代码的问题吗?

To give some back ground about the format of the string The following string represents: the following file path.

要给出一些关于字符串格式的背景,下面的字符串表示:下面的文件路径。

"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 

dir
 subdir1
    file1.ext
    subsubdir1
 subdir2
    subsubdir2
        file2.ext

2 个解决方案

#1


1  

Simplifying your existing code gives the following, which returns the correct result for both of your examples:

简化您现有的代码,如下所示,它将为您的两个示例返回正确的结果:

public static int lengthLongestPath(String input) {
    if (input.length() == 0)
        return 0;
    Map<Integer, Integer> map = new HashMap<>();
    int maxLength = 0;
    String[] paths = input.split("\n");
    for (String path : paths) {
        String dirOrFile = path.replaceFirst("\\t*", "");
        int level = path.lastIndexOf("\t") + 1;

        int prefixLength = 0;
        if (level > 0) {
            prefixLength = map.get(level - 1);
        }

        int pathLength = prefixLength + dirOrFile.length();
        if (dirOrFile.contains(".")) {
            maxLength = Math.max(pathLength + level, maxLength);
        }
        map.put(level, pathLength);
    }
    return maxLength;
}

#2


0  

I think you may be over-complicating things. Due to the string's format, you can't "go back" to a directory you've already left, so you don't need to keep a map. An alternate implementation could be to split the string by the \n character and use a list to keep track of where you are in the path, where the number of \t characters denotes the position you need to keep:

我认为你可能过于复杂了。由于字符串的格式,您不能“返回”到您已经离开的目录,所以您不需要保留一个地图。另一个实现可能是将字符串拆分为\n字符,并使用一个列表来跟踪您在路径中的位置,其中\t字符的数量表示您需要保存的位置:

public static int lengthLongestPath(String input) {
    int maxLen = 0;
    List<String> currPath = new LinkedList<>();
    String[] parts = input.split("\n");
    for (String part : parts) {
        int numTabs = Math.max(0, part.lastIndexOf('\t'));
        part = part.substring(numTabs + 1);
        currPath = currPath.subList(0, numTabs);
        currPath.add(part);
        int curLen = currPath.stream().mapToInt(String::length).sum();
        maxLen = Math.max(maxLen, curLen);
    }
    return maxLen;
}

#1


1  

Simplifying your existing code gives the following, which returns the correct result for both of your examples:

简化您现有的代码,如下所示,它将为您的两个示例返回正确的结果:

public static int lengthLongestPath(String input) {
    if (input.length() == 0)
        return 0;
    Map<Integer, Integer> map = new HashMap<>();
    int maxLength = 0;
    String[] paths = input.split("\n");
    for (String path : paths) {
        String dirOrFile = path.replaceFirst("\\t*", "");
        int level = path.lastIndexOf("\t") + 1;

        int prefixLength = 0;
        if (level > 0) {
            prefixLength = map.get(level - 1);
        }

        int pathLength = prefixLength + dirOrFile.length();
        if (dirOrFile.contains(".")) {
            maxLength = Math.max(pathLength + level, maxLength);
        }
        map.put(level, pathLength);
    }
    return maxLength;
}

#2


0  

I think you may be over-complicating things. Due to the string's format, you can't "go back" to a directory you've already left, so you don't need to keep a map. An alternate implementation could be to split the string by the \n character and use a list to keep track of where you are in the path, where the number of \t characters denotes the position you need to keep:

我认为你可能过于复杂了。由于字符串的格式,您不能“返回”到您已经离开的目录,所以您不需要保留一个地图。另一个实现可能是将字符串拆分为\n字符,并使用一个列表来跟踪您在路径中的位置,其中\t字符的数量表示您需要保存的位置:

public static int lengthLongestPath(String input) {
    int maxLen = 0;
    List<String> currPath = new LinkedList<>();
    String[] parts = input.split("\n");
    for (String part : parts) {
        int numTabs = Math.max(0, part.lastIndexOf('\t'));
        part = part.substring(numTabs + 1);
        currPath = currPath.subList(0, numTabs);
        currPath.add(part);
        int curLen = currPath.stream().mapToInt(String::length).sum();
        maxLen = Math.max(maxLen, curLen);
    }
    return maxLen;
}