sweet1cris

Untitled

Feb 9th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1.  
  2.  
  3. public class Solution {
  4.     /**
  5.      * @param input an abstract file system
  6.      * @return return the length of the longest absolute path to file
  7.      */
  8.     public int lengthLongestPath(String input) {
  9.         // Write your code here
  10.         int result = 0;
  11.         int[] path = new int[input.length() + 2];
  12.         String[] st = input.split("\n");
  13.         for (String line : st){
  14.             String name = line.replaceAll("(\t)+", "");
  15.             int depth = line.length() - name.length();
  16.             if(name.contains("."))
  17.                 result = Math.max(result, path[depth] + name.length());
  18.             else
  19.                 path[depth + 1] = path[depth] + name.length() + 1;
  20.         }
  21.         return result;
  22.     }
  23. }
  24.  
  25. // version: 高频题班
  26. public class Solution {
  27.     /*
  28.     * @param input an abstract file system
  29.     * @return return the length of the longest absolute path to file
  30.     */
  31.     public int lengthLongestPath(String input) {
  32.         // Write your code here
  33.         if (input.length() == 0) {
  34.             return 0;
  35.         }
  36.         int ans = 0;
  37.         int[] level_size = new int[input.length() + 1];
  38.  
  39.         for (String line : input.split("\n")) {
  40.             int level = line.lastIndexOf('\t') + 2;
  41.             int len = line.length() - (level - 1);
  42.             if (line.contains(".")) {
  43.                 ans = Math.max(ans, level_size[level - 1] + len);
  44.             } else {
  45.                 level_size[level] = level_size[level - 1] + len + 1;
  46.             }
  47.         }
  48.         return ans;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment