class Solution {
public:
int trap(vector<int>& height) {
int res = 0;
int maxaddr = 0;
for (int i = 0; i < height.size(); i++)
{
if (height[i] > height[maxaddr])
{
maxaddr = i;
}
}
int left = 0;
for (int i = left + 1; i < maxaddr; i++)
{
if (height[i] < height[left])
{
res += height[left] - height[i];
}
else
left = i;
}
int right = height.size() - 1;
for (int j = right - 1; j > maxaddr; j--)
{
if (height[j] < height[right])
{
res += height[right] - height[j];
}
else
right =j;
}
return res;
}
};
LeetCode接雨水
最新推荐文章于 2024-07-23 16:17:06 发布