leetcode每日一題 388. 檔案的最長絕對路徑
阿新 • • 發佈:2022-04-20
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;
}
}