题解 | #没有重复项数字的所有排列#
接雨水问题
http://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
import java.util.*; public class Solution { /** * max water * @param arr int整型一维数组 the array * @return long长整型 */ public long maxWater (int[] arr) { // write code here if (arr == null || arr.length < 3) { return 0; } int left = 0, right = arr.length - 1; long maxLeft = 0, maxRight = 0, ans = 0; /* 3,1,2,5,2,4 */ while (left <= right) { if (maxLeft < maxRight) { ans += Math.max(0, maxLeft - arr[left]); maxLeft = Math.max(maxLeft,arr[left]); left++; } else { ans += Math.max(0, maxRight - arr[right]); maxRight = Math.max(maxRight,arr[right]); right--; } } return ans; } }