42. Trapping Rain Water

Problem's Link

----------------------------------------------------------------------------

Mean:

在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.

analyse:

首先找出最高的积木,然后从前往后一直扫到最高积木,从后往前一直扫到最高积木,两部分体积相加即可.

Time complexity: O(N)

view code

;
       ;
       ;;
           ;
           );
       ;
           ;
           );;
}
/*

*/

2.方法二:时间O(n),空间(1)

;
       ;
       ;
       while(left<right)
       {
           if(height[left]<=height[right])
           {
               if(height[left]>maxLeft)
                   maxLeft=height[left];
               res+=maxLeft-height[left];
               left++;
           }
           else
           {
               if(height[right]>maxRight)
                   maxRight=height[right];
               res+=maxRight-height[right];
               right--;
           }
       }
       return res;
   }
};

LeetCode - 42. Trapping Rain Water的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

  9. Java [Leetcode 42]Trapping Rain Water

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

随机推荐

  1. ldr和adr在使用标号表达式作为操作数的区别

    ARM汇编有ldr指令以及ldr.adr伪指令,他门都可以将标号表达式作为操作数,下面通过分析一段代码以及对应的反汇编结果来说明它们的区别. ldr     r0, _start adr     r0 ...

  2. 超棒的 15 款 Bootstrap UI 编辑器

    自从 2011 年 Mark Otto 和 Jacob Thornton 开发了  Bootstrap,我们第一次接触并熟知了 Bootstrap .这些都归功于  Twitter!从那以后,它就非常 ...

  3. Android 使用java.net.socket 的接收问题

    // 初始化socketsocket = new Socket(InetAddress.getByName(sip), sport);InputStream sin = socket.getInput ...

  4. 便宜有好货:Oracle免费的便捷Web应用开发框架

    APEX 总体来说,APEX是我见过最便捷最高效的开发框架,用起来比PHP还舒服.上手简单,学习成本极低,曾经有个做行政的小女生,在我指导下两天就可以开发出简单的审批管理站点.如果企业要做一些内部应用 ...

  5. A Simple MVVM Example[Forward]

    In my opinion, if you are using WPF or Silverlight you should be using the MVVM design pattern. It i ...

  6. Hadoop 2.4.1 登录认证配置小结

    1.简单模式 这种模式,配置简单,使用简单. core-site.xml添加 <property> <name>hadoop.security.authorization< ...

  7. JS - IE中没有console定义

    由于IE中没有Console相关定义,所以不能使用它输出打印信息,且会出现脚本中断. 所以在IE中务必去掉(注释掉)console相关脚本代码.

  8. easyui plugin——etreegrid:CRUD Treegrid

    昨天写了一个ko+easyui的同样的实现,感觉写的太乱,用起来十分麻烦,于是今天照着edatagrid,写了一个etreegrid,这样再用ko绑定就方便多了. 使用很简单,$(tableId).e ...

  9. saiku 分布式实践

    saiku比较吃内存,一旦人多了,那么内存可能不够,所以会考虑主从结构,分担压力.为了保证数据的稳定性,也会有类似的考虑,那么问题来了,如何实现saiku的分布式搭建哪? 我阅读了一些国内的文章,没有 ...

  10. javascript 无语的==

    今天面试不小心掉进坑了,大公司特别喜欢考javascript,而且专门挑很tricky的case. javascipt的==简直就是黑魔法,以前偷懒总是用,感觉也没有问题,可是准备面试就需要有寻根问底 ...