GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/TrappingRainWater.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathTrappingRainWater.javaMore file actions 23 lines (21 loc) · 820 Bytes / Copy pathTrappingRainWater.javaFile metadata and controls23 lines (21 loc) · 820 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /** * 核心思路就是对于每根柱子,找到其左边最高的柱子和右边最高的柱子,构成一个桶,形成一个水平面,然后对该柱子形成的高度差就是能装的水 */ public class TrappingRainWater { // 耗时24ms public int trap(int[] height) { int[] left = new int[height.length]; int[] right = new int[height.length]; for (int i = 1; i < height.length; i++) { left[i] = Math.max(left[i - 1], height[i - 1]); } for (int i = height.length - 2; i >= 0; i--) { right[i] = Math.max(right[i + 1], height[i + 1]); } int rain = 0; for (int i = 1; i < height.length - 1; i++) { rain += Math.max(0, Math.min(left[i], right[i]) - height[i]); } return rain; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |