JAVA------------------Easy Version To UnderStand!!!!!!


  • 0
    H
    public static int trap(int a[]) {
    	if (a == null || a.length <= 0)
    		return 0;
    	int n = a.length;
    	int low = 0, high = n - 1, secondMaxHeight = -1, area = 0;//
    	while (low <= high) {
    		if (a[low] < a[high]) {
    			if (a[low] > secondMaxHeight)
    				secondMaxHeight = a[low];
    			area += secondMaxHeight - a[low];
    			low++;
    
    		} else {
    			if (a[high] > secondMaxHeight)
    				secondMaxHeight = a[high];
    			area += secondMaxHeight - a[high];
    			high--;
    		}
    	}
    
    	return area;
    }

  • 1

    if a is null, your second line will throw exception......


  • 0
    H

    i have considered it!!


  • 0
    Y

    He means if a is null, your second line int n = a.length; a.lenght will throw exception.
    Not the third line. if (a == null || n <= 0) , has no use.


  • 0
    H

    Oh,thank you, I see it.I have corrected it!


Log in to reply
 

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