标签:div href desc ret return log == odi mil
package leetcode_50; /*** * * @author pengfei_zheng * 求积水的体积 */ public class Solution42 { public static int trap(int[] height) { if (height.length <= 2) return 0; int max = -1; int maxIndex = 0; for (int i = 0; i < height.length; i++) { if (height[i] > max) { max = height[i]; maxIndex = i; } } int leftMax = height[0]; int water = 0; for (int i = 1; i < maxIndex; i++) { if (height[i] > leftMax) { leftMax = height[i]; } else { water += leftMax - height[i]; } } int rightMax = height[height.length - 1]; for (int i= height.length - 2; i > maxIndex; i--) { if (height[i] > rightMax) { rightMax = height[i]; } else { water += rightMax - height[i]; } } return water; } public static void main(String[]args){ // int []height={0,1,0,2,1,0,1,3,2,1,2,1}; int []height={10,0,11,0,10}; System.out.println(trap(height)); } }
LeetCode 42 Trapping Rain Water(积水体积)
标签:div href desc ret return log == odi mil
原文地址:http://www.cnblogs.com/zpfbuaa/p/6537783.html