-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path363.h
31 lines (31 loc) · 845 Bytes
/
363.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
/*
* @param heights: a list of integers
* @return: a integer
*/
int trapRainWater(vector<int> &heights) {
// write your code here
int _max;
int water = 0;
int maxIndex = 0;
//找到数组中最大值
for(int i = 1; i < heights.size(); i++){
if(heights[i] > heights[maxIndex])
maxIndex = i;
}
//前后遍历
_max = 0;
for(int i = 0; i < maxIndex; i++){
_max = max(_max, heights[i]);
water += _max - heights[i];
}
//这里不从能maxIndex -> size-1!
_max = 0;
for(int i = heights.size()-1; i > maxIndex; i--){
_max = max(_max, heights[i]);
water += _max - heights[i];
}
return water;
}
};