388. Longest Absolute File Path

白昼怎懂夜的黑 提交于 2019-12-06 13:50:24
class Solution {
    public int lengthLongestPath(String input) {
        String [] arr=input.split("\n");
        int [] lens= new int[arr.length];
       
        int max=0;
        for(String s:arr){
            int lastBackT= s.lastIndexOf("\t");
            int level = lastBackT+1; 
            int parentLen = level==0? 0: lens[level-1]+1;
            int nameLen = s.length()-lastBackT-1;
            lens[level] = parentLen+nameLen;
            if(s.contains("."))
            {
                max = Math.max(max,lens[level]);
            }
        }
        
        return max;
    }
}

The first thought is DFS, however, we're able to deal it with array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!