/**
     * 此为暴力解法
     * Find two lines, which together with x-axis forms a container, such that the container contains the most water.
     * @param height 高度
     * @return 面积
     */
    public int _maxArea(int[] height) {
        int area = 0;
        for(int i = 0; i < height.length; i++) {
            for(int j = i+1; j < height.length; j++) {
                int h = height[j] < height[i] ? height[j] : height[i];
                area = area > (j-i)*(h) ? area : (j-i)*(h);
            }
        }
        return area;
    }

    public int maxArea(int[] height) {
        int area = 0;
        // 两个指针
        int i = 0;
        int j = height.length-1;
        while(i < j) {
            int h = Math.min(height[i], height[j]);
            area = Math.max(area, (j - i) * (h));
            if(height[i] <= height[j]) {
                i++;
            } else {
                j--;
            }
        }
        return area;
    }

Array - Container With Most Water的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  3. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  4. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

  5. leetcode-algorithms-11 Container With Most Water

    leetcode-algorithms-11 Container With Most Water Given n non-negative integers a1, a2, ..., an , whe ...

  6. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  7. Container With Most Water(LintCode)

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  8. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  9. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

随机推荐

  1. c# sleep 例子-线程挂起

    using System; using System.Threading; public class arr { public static void Main() { //int[] arr; // ...

  2. 用matplotlib画线

    1:matplotlib基础 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形[1] . 通过 Matplotlib,开发者可以 ...

  3. JavaScript中两个数组的拼接

    方法一:使用for循环 var arr = ['tom', 'jerry']; var arr2 = [1, 2]; for(var i=0; i<arr2.length; i++){ arr. ...

  4. 最优化理论-Simplex线性规划

     Sorry,各位,现在这里面啥也没,之所以开这篇文章,是防止以后用得到:现在研究这些,总感觉有些不合适,本人还不到那个层次:如果之后有机会继续研究simplex-线性规划问题,再回来参考下面的链接进 ...

  5. java接口中成员变量和方法的默认修饰符(转)

    Java的interface中,成员变量的默认修饰符为:public static final 所以我们在interface中定义成员变量的时候,可以 1:public static final St ...

  6. ue4 weapon

    UE4版本4.17,不同版本api可能有差异 静态 1 在骨骼上加socket 在socket上右键-添加浏览资源-找到要添加的那个道具(这个只用来看效果,调位置,不会显示到最终效果中),调整sock ...

  7. 洛谷P3757 [CQOI2017]老C的键盘

    传送门 首先可以直接把整个序列建成一个完全二叉树的结构,这个应该都看得出来 然后考虑树形dp,以大于为例 设$f[i][j]$表示$i$这个节点在子树中排名第$j$位时的总方案数(因为实际只与相对大小 ...

  8. UVALive - 3695 Distant Galaxy

    InputThere are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ ...

  9. Javascript 给table动态增、删除行

    操作 HTML DOM Table 对象 即可 http://www.runoob.com/jsref/dom-obj-table.html 动态给一个元素焦点,用focus()方法

  10. mysql导入文件

    手里有一个web源码工程文件夹 mysql导入文件: 新建连接,名称随意,用修改设置的用户密码登录,我的连接名称是eee 右击information_schema,建立数据库,数据库名称源码文件名,字 ...