Skip to content

Commit acf2a52

Browse files
committed
思路!!
1 parent 311a9e8 commit acf2a52

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

small-trick/Trapping Rain Water

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)