From 1ab8f4eaed17d322b9369be46cb831fd63c2ddb4 Mon Sep 17 00:00:00 2001 From: Gik7 <1695142971@qq.com> Date: Thu, 18 Sep 2025 20:13:58 +0800 Subject: [PATCH] 20250918text2 --- ..._\351\231\210\350\264\236\347\222\207.cpp" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "topic01/submit/LC42_\351\231\210\350\264\236\347\222\207.cpp" diff --git "a/topic01/submit/LC42_\351\231\210\350\264\236\347\222\207.cpp" "b/topic01/submit/LC42_\351\231\210\350\264\236\347\222\207.cpp" new file mode 100644 index 0000000..71f35e7 --- /dev/null +++ "b/topic01/submit/LC42_\351\231\210\350\264\236\347\222\207.cpp" @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +class Solution { +public: + int trap(vector& height) { + int n = height.size(); + if (n == 0) return 0; + + int left = 0, right = n - 1; + int left_max = height[left], right_max = height[right]; + int water = 0; + + while (left < right) { + if (left_max < right_max) { + left++; + left_max = max(left_max, height[left]); + water += left_max - height[left]; + } + else { + right--; + right_max = max(right_max, height[right]); + water += right_max - height[right]; + } + } + + return water; + } +}; \ No newline at end of file -- Gitee