java recursive method 5ms


  • 0
    C
    public class Solution {
        private int max = 0;
        private int recu(String in, int s, int len, int num){
            boolean flag = false;
            int i=s;
            for(; i < in.length() && in.charAt(i) != '\n'; i++){
                if(in.charAt(i) == '.')
                    flag = true;
                len++;
            }
            i++;
            if(flag){
                max = Math.max(max,len);
                return i;
            }
            len++;
            num++;
            for(; i < in.length();){
                int count = 0;
                int j=i;
                while(in.charAt(j) == '\t'){
                    count++;
                    j++;
                }
                if(count < num)
                    return i;
                i = recu(in,j,len,num);
            }
            return in.length();
        }
        public int lengthLongestPath(String input) {
            max = 0;
            for(int i=0; i < input.length();)
                i = recu(input,i,0,0);
            return max;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.