題目鏈接: LeetCode 42 — Trapping Rain Water
問(wèn)題分析: 貪心思路,很簡(jiǎn)單。
AC代碼:
public class PRoblem42 { public static void main(String[] args) { // TODO Auto-generated method stub/ int[] a={}; System.out.println(trap(a)); } public static int trap(int[] height) { int i=0,j=height.length-1; if(i>j) return 0; int maxleft = height[i]; int maxright= height[j]; int value =0; while(i<j){ if(height[i]<=height[j]){ i++; if(height[i]<maxleft){ value+=maxleft-height[i]; continue; } else{ maxleft=height[i]; continue; } } else{ j--; if(height[j]<maxright){ value+=maxright-height[j]; continue; } else{ maxright=height[j]; continue; } } } return value; }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注