publicclassMain{publicstaticvoidmain(String[] args){int a[]={0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trap(a));}publicstaticinttrap(int[] a){// 陣列中最大值的下標int max_index =0;// 最大值左側的極大值int l =0;// 最大值右側的極大值int r =0;int sum =0;for(int i =0; i < a.length; i++){
max_index = a[i]> a[max_index]? i : max_index;}// System.out.println(max_index);// 左側的和for(int i =0; i < max_index; i++){if(a[i]< l)
sum += l - a[i];else
l = a[i];}// 右側的和for(int i = a.length-1; i > max_index; i--){if(a[i]< r)
sum += r - a[i];else
r = a[i];}return sum;}}