My Solution With Stack


  • 0
    B
    public int lengthLongestPath(String input) {
    	String [] paths = input.split("\n");
    	if (!paths[paths.length-1].contains(".")) {
    		return 0;
    	}
    	Stack<Integer> stack = new Stack<>();
    	int max = 0;
    	int tmpMax = max;
    	for(int i=0;i<paths.length;i++){
    	//	System.out.print(paths[i]);	
    		int[] top ;
    		top = getSubLength(paths[i]);
    	//	System.out.println("  t:"+top[0]+" t1:"+top[1]);
    		while(!stack.isEmpty()&&top[0]<stack.size()){
    			tmpMax -=(stack.pop());
    		}
    		tmpMax+=top[1]+1;
    		if (paths[i].contains(".")) {
    			max = Math.max(tmpMax, max);
    		}
    		
    	//	System.out.println("max:"+max);
    		stack.push(top[1]+1);
    	}
    	return max-1;
    }
    public int[] getSubLength(String string){
    	
    	int len = string.length();
    	int res[] = new int[2];
    	int index = string.indexOf("\t", 0);
    	while(index!=-1){
    		res[0]+=1;
    		index =	string.indexOf("\t", index+1);
    	}
    	res[1] = len - string.lastIndexOf("\t")-1 ; 
    	return res;
    }

Log in to reply
 

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