{"id":2886,"date":"2020-02-06T13:44:53","date_gmt":"2020-02-06T05:44:53","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2886"},"modified":"2020-02-06T13:44:53","modified_gmt":"2020-02-06T05:44:53","slug":"%e3%80%90leetcode%e3%80%910042-%e6%8e%a5%e9%9b%a8%e6%b0%b4","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2886\/","title":{"rendered":"\u3010LeetCode\u30110042 \u63a5\u96e8\u6c34"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/trapping-rain-water\/\">https:\/\/leetcode-cn.com\/problems\/trapping-rain-water\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def trap(self, height):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> height: List[int]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: int\n        \"\"\"\n        <\/em>n = len(height)\n        if n &lt;= 2:\n            return 0\n        left = 0\n        right = n - 1\n        sum = 0\n        while left &lt; right:\n            if height[left] != 0 and height[right] != 0:\n                min_flag = min(height[left], height[right])\n                for i in range(left, right + 1):\n                    if height[i] &lt; min_flag:\n                        sum += min_flag - height[i]\n                        height[i] = min_flag\n            if height[left] &lt; height[right]:\n                left += 1\n            else:\n                right -= 1\n        return sum\nif __name__ == '__main__':\n    height = [0, 1, 0]\n    print(Solution().trap(height))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a \u7c7b\u4f3c\u4e8e0011\uff0c\u5de6\u53f3\u6307\u9488\u5411\u4e2d\u95f4\u9760\u62e2\uff0c\u627e\u5230\u5f53\u524d\u53ef\u8fbe\u5bb9\u79ef\u7136\u540e\u6539\u53d8\u6570\u7ec4\u503c\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/trapping-rain-wat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1381,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2886"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2886"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2886\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2886"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}