Java实现-接雨水
程序员文章站
2022-06-29 08:44:32
...
public class Solution {
/**
* @param heights: an array of integers
* @return: a integer
*/
public int trapRainWater(int[] heights) {
// write your code here
int left = 0, right = heights.length - 1;
int res = 0;
if(left >= right)
return res;
int leftheight = heights[left];
int rightheight = heights[right];
while(left < right) {
if(leftheight < rightheight) {
left ++;
if(leftheight > heights[left]) {
res += (leftheight - heights[left]);
} else {
leftheight = heights[left];
}
} else {
right --;
if(rightheight > heights[right]) {
res += (rightheight - heights[right]);
} else {
rightheight = heights[right];
}
}
}
return res;
}
}
上一篇: shell-date
下一篇: Linux系统下JDK11安装与配置