1 条题解
-
0Guest MOD
-
1
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int trap(int* height, int heightSize) { int ans = 0; int left = 0; int right = heightSize - 1; int premax = 0; int max = 0; int i = 0; while (left < right) { if (height[left] > height[right] && height[right] > max) { premax = max; max = height[right]; for (i = left + 1; i <= right - 1; i++) { if (height[i] <= premax) ans = ans - premax + max; if (height[i] > premax&&height[i] < max) ans = ans + max - height[i]; } right--; } if (height[left] <= height[right] && height[left] > max) { premax = max; max = height[left]; for (i = left + 1; i <= right - 1; i++) { if (height[i] <= premax) ans = ans - premax + max; if (height[i] > premax&&height[i] < max) ans = ans + max - height[i]; } left++; } if (height[left] <= max) { left++; } if (height[right] <= max) { right--; } } return ans; } int main() { int height[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 }; int heightSize = sizeof(height) / sizeof(int); int ans = trap(height, heightSize); printf("%d", ans); }
牛不牛逼?
- 1
信息
- ID
- 1884
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 4
- 已通过
- 1
- 上传者