We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 311a9e8 commit acf2a52Copy full SHA for acf2a52
small-trick/Trapping Rain Water
@@ -0,0 +1,25 @@
1
+ // 找出最大值,右边和左边单独处理!
2
+
3
+ class Solution {
4
+ public:
5
+ int trap(int A[], int n) {
6
+ int maxIndex = 0;
7
+ for(int i = 0;i < n;i++)
8
+ if(A[i] > A[maxIndex])
9
+ maxIndex = i;
10
+ int leftMax = 0,rightMax = 0,aera = 0;
11
+ for(int i = 0;i < maxIndex;i++)
12
+ {
13
+ if(A[i] > leftMax)
14
+ leftMax = A[i];
15
+ aera += leftMax - A[i];
16
+ }
17
+ for(int j = n-1;j > maxIndex ;--j) // 中间就是靠山,从右边也是要按照中间是最后的思路来
18
19
+ if(A[j] > rightMax)
20
+ rightMax = A[j];
21
+ aera += rightMax - A[j];
22
23
+ return aera;
24
25
+ };
0 commit comments