leetcode每日一题 388. 文件的最长绝对路径

leetcode每日一题 388. 文件的最长绝对路径

1.先考虑各种情况,然后写公共情况,代码没优化,leetcode跑和本地测试结果不一样,没去往下做了
class Solution {
  public int lengthLongestPath(String input) {
      //不是目录的情况
      if(input.indexOf('\\') == -1){
          return 0;
      }
      //不是多级目录
      if(input.indexOf("\\\\t") == -1){
          int max = 0;
          String[] split = input.split("\\\\n");
          for (String s : split) {
              max = Math.max(max,s.length());
          }
          return max;
      }
      //多级目录
      int max = 0;
      String[] split = input.split("\\\\n");
      for (String s : split) {
          max = Math.max(max,f(input, "\\\\t"));
      }
      return max;
  }

  private int f(String input, String s) {
      if (input.indexOf("\\") == -1) {
          return input.length();
      }
      int max = 0;
      String[] split = input.split("\\\\n" + s + "(?!\\\\)");
      System.out.println(Arrays.toString(split));
      for (int i = 1; i < split.length; i++) {
          max = Math.max(max, f(split[i], s + "\\\\t"));
      }
      return max + split[0].length() + 1;
  }
}

 

 

posted @ 2022-04-20 20:07  java架构师1  阅读(25)  评论(0)    收藏  举报