用C++单调栈写接雨水,并加上注释
时间: 2023-08-31 17:25:38 浏览: 73
### 回答1:
好的,以下是使用C语言编写单调栈解决接雨水问题的代码和注释:
```c
#include <stdio.h>
int trap(int* height, int heightSize){
int result = 0; // 存储接雨水的量
int* stack = (int*) malloc(heightSize * sizeof(int)); // 创建单调栈
int top = -1; // 栈顶指针初始化为-1
for (int i = 0; i < heightSize; i++) {
while (top >= 0 && height[stack[top]] < height[i]) { // 当前元素比栈顶元素大,出栈计算雨水量
int mid = stack[top--]; // 栈顶元素出栈
if (top < 0) { // 栈空,不能计算雨水量
break;
}
int left = stack[top]; // 左边界为栈顶元素
int right = i; // 右边界为当前元素
int h = height[left] < height[i] ? height[left] : height[i]; // 高度取左右两边较小的值
result += (right - left - 1) * (h - height[mid]); // 计算雨水量
}
stack[++top] = i; // 元素入栈
}
free(stack); // 释放单调栈的内存空间
return result; // 返回计算结果
}
int main() {
int height[] = {0,1,0,2,1,0,1,3,2,1,2,1}; // 测试用例
int heightSize = sizeof(height) / sizeof(height[0]); // 数组长度
int res = trap(height, heightSize); // 调用接雨水函数计算雨水量
printf("%d\n", res); // 输出结果
return 0;
}
```
注释如下:
1. `int trap(int* height, int heightSize)`: 接雨水函数,参数是柱子高度数组和数组长度。
2. `int result = 0;`: 存储接雨水的量,初始化为0。
3. `int* stack = (int*) malloc(heightSize * sizeof(int));`: 创建单调栈,存储柱子的下标。
4. `int top = -1;`: 栈顶指针初始化为-1,表示栈为空。
5. `for (int i = 0; i < heightSize; i++) {`: 遍历柱子高度数组。
6. `while (top >= 0 && height[stack[top]] < height[i]) {`: 如果栈不为空且当前元素比栈顶元素大,说明可以计算雨水量,进入循环。
7. `int mid = stack[top--];`: 栈顶元素出栈,存储到`mid`中。
8. `if (top < 0) { break; }`: 如果栈空,不能计算雨水量,退出循环。
9. `int left = stack[top];`: 左边界为栈顶元素。
10. `
### 回答2:
```cpp
#include <stdio.h>
#define MAXN 1000
int stack[MAXN], top = -1; // 定义一个栈保存每个柱子
int trap(int* height, int heightSize) {
int ans = 0, current = 0; // 定义一个变量保存最终结果和当前所处位置
while (current < heightSize) {
while (top != -1 && height[current] > height[stack[top]]) { // 当前柱子的高度大于栈顶柱子的高度
int h = height[stack[top]]; // 弹出栈顶柱子作为坑的底部
top--; // 更新栈顶指针
if (top == -1) // 栈为空,无法形成坑
break;
int distance = current - stack[top] - 1; // 计算坑的宽度
int min = height[current] < height[stack[top]] ? height[current] : height[stack[top]]; // 计算坑的高度
ans += distance * (min - h); // 计算坑的面积并累加到结果中
}
top++; // 当前柱子入栈
stack[top] = current; // 保存当前柱子的索引
current++; // 更新当前位置
}
return ans; // 返回结果
}
int main() {
int height[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; // 柱子高度
int heightSize = sizeof(height) / sizeof(height[0]); // 柱子数量
int result = trap(height, heightSize); // 调用接雨水函数
printf("接雨水的总量为:%d", result); // 输出结果
return 0;
}
```
以上代码是用C语言实现的接雨水问题的单调栈解法,注释已加在每个关键步骤的旁边。主要思路是遍历数组,利用单调递减的栈来保存每个可能形成坑的柱子的索引。当当前柱子的高度大于栈顶柱子的高度时,说明形成了一个坑,计算坑的宽度和高度,并累加到结果中。最后返回结果即为可接到的雨水的总量。
### 回答3:
接雨水问题是一个经典的算法问题,在应用中经常遇到。下面是使用C语言和单调栈来解决接雨水问题的代码实现,并附上注释解释每一步的操作。
```c
#include <stdio.h>
int min(int a, int b) {
return a < b ? a : b;
}
// 使用单调栈解决接雨水问题
int trap(int* height, int heightSize) {
int ans = 0;
int current = 0; // 当前位置
int stack[heightSize]; // 定义一个数组作为栈
int top = -1; // 初始化栈顶指针
while (current < heightSize) {
// 如果栈不空且当前高度大于栈顶所存的高度
while (top != -1 && height[current] > height[stack[top]]) {
int h = height[stack[top]]; // 获取栈顶存储的高度
top--; // 弹出栈顶元素
if (top == -1) { // 如果栈空了,说明没有左边的边界,无法接住雨水,直接结束本次循环
break;
}
int distance = current - stack[top] - 1; // 计算当前位置距离前一个位置的距离
int bounded_height = min(height[current], height[stack[top]]) - h; // 计算接住雨水的高度
ans += distance * bounded_height; // 累加接住雨水的面积
}
top++; // 将当前位置入栈
stack[top] = current;
current++; // 继续遍历下一个位置
}
return ans;
}
int main() {
int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
int heightSize = sizeof(height) / sizeof(height[0]);
int result = trap(height, heightSize);
printf("接雨水的面积是:%d\n", result);
return 0;
}
```
以上代码中,我们首先定义了一个min函数用于取两个数中的较小值。然后我们使用一个while循环遍历整个输入数组。在每次循环中,我们通过比较当前的高度和栈顶的高度来判断是否可以接住雨水。如果可以接住雨水,我们就计算接住的雨水面积并累加到结果中。
在主函数中,我们给出了一个示例的高度数组,并调用trap函数计算接雨水的面积。最后打印出结果。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)