题目描述

给定n个非负整数a1,a2,…,an,其中每个数字表示坐标(i, ai)处的一个点。以(i,ai)和(i,0)(i=1,2,3...n)为端点画出n条直线。你可以从中选择两条线与x轴一起构成一个容器,最大的容器能装多少水?
注意:你不能倾斜容器
例如:
输入 [1,8,6,2,5,4,8,3,7]
输出: 49

Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ). nvertical lines are drawn such that the two endpoints of line i is at (i, ai ) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
示例1

输入

复制

[1,8,6,2,5,4,8,3,7]

输出

复制

49

class Solution {
public:
    /**
     *
     * @param height int整型vector
     * @return int整型
     */
    int maxArea(vector<int>& height) {
        // write code here
        if (height.size()<2)//数据太少不能构成容器
            return 0;
        int i=0,j=height.size()-1;
        int maxArea=0;
        int tmpArea=0;
        while (i<j){
            tmpArea=min(height[i],height[j])*(j-i);
            if (tmpArea>maxArea)
                maxArea=tmpArea;
            if (height[i]<height[j])
                ++i;
            else
                --j;
        }
        return maxArea;
    }
};
class Solution {
public:
    /**
     *
     * @param height int整型vector
     * @return int整型
     */
    int maxArea(vector<int>& height) {
        // write code here
        int l,r,Max=-9999;
        for (l=0,r=height.size()-1;l<r;){
            Max=max(Max,(r-l)*min(height[l],height[r]));
            height[l]<height[r]?l++:r--;
            
            
        }
        return Max;
    }
};
import java.util.*;

public class Solution {
    /**
     *
     * @param height int整型一维数组
     * @return int整型
     */
    public int maxArea (int[] height) {
        // write code here
        if (height.length<2){
            return 0;
            
        }
        int left=0;
        int right=height.length-1;
        int maxV=0;
        while (left<right){
            int v=Math.min(height[left],height[right])*(right-left);
            maxV=Math.max(v,maxV);
            if (height[left]<height[right]){
                left++;
            }else {
                right--;
            }
        }
        return maxV;
    }
}
#
#
# @param height int整型一维数组
# @return int整型
#
class Solution:
    def maxArea(self , height ):
        # write code here
        left=0
        right=len(height)-1
        V=0
        while left<right:
            V=max(V,min(height[left],height[right])*(right-left))
            if height[left]<height[right]:
                left+=1
            else:
                right-=1
        return V
            
            
          

leetcode138container-with-water的更多相关文章

  1. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  2. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

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

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

  5. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

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

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

  7. 【leetcode】Container With Most Water

    题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. [LintCode] Trapping Rain Water 收集雨水

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

  9. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  10. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

随机推荐

  1. IntelliJ IDEA 调试 Java 8 Stream,实在太香了!

    前段时间,栈长发布了一篇关于 Java 8 Optional.map 的实用文章,留言区就有的人说 Java 8 的语法糖不方便调试,还要视情况使用. 留言区也有人说 IntelliJ IDEA 早已 ...

  2. UIScrollView无法滚动以及超出屏幕的内容无法进行编辑

    通过UITextView实现简单的富文本内容编辑,功能按钮包装时遇到些问题,简单记录如下 1.通过UIToolbar将字体设置功能罗列出来,由于功能过多,通过UIScrollView进行滑动控制 1) ...

  3. Angluar2 项目搭建

    一 使用 Angular CLI 官方脚手架 1.安装 cli npm install -g @angular/cli 2.创建工作空间和初始应用 ng new my-app 二 tsLint 代码格 ...

  4. 推荐一款IDEA神器!一键查看Java字节码以及其他类信息

    由于后面要分享的一篇文章中用到了这篇文章要推荐的一个插件,所以这里分享一下.非常实用!你会爱上它的! 开始推荐 IDEA 字节码查看神器之前,先来回顾一下 Java 字节码是啥. 何为 Java 字节 ...

  5. 微信小程序适配iPhone X

    1.获取设备型号 App({ // 全局数据 globalData: { // 其他数据定义 ... isIPX: false, // 当前设备是否为 iPhone X }, // 小程序启动入口 o ...

  6. 本溪6397.7539(薇)xiaojie:本溪哪里有xiaomei

    本溪哪里有小姐服务大保健[微信:6397.7539倩儿小妹[本溪叫小姐服务√o服务微信:6397.7539倩儿小妹[本溪叫小姐服务][十微信:6397.7539倩儿小妹][本溪叫小姐包夜服务][十微信 ...

  7. Go net/http包

    net/http包 net/http是Go语言的内置包,它可以来创建HTTP客户端与服务端. 并且由net/http包创建的服务端性能十分高效,甚至不用nginx部署. client端 GET请求 以 ...

  8. windows搭建SVN服务

    下载`TortoiseSVN 官网下载址:https://www.visualsvn.com/visualsvn/download/tortoisesvn/ 根据自己系统环境选择 安装Tortoise ...

  9. 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)

    1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...

  10. ThreadLocal使用说明

    让变量只能在这个线程内被读写,在其他线程内无法被访问.以键值对存放变量,并继承弱应用,内存随时会被回收,用完要remove不然会内存泄漏,使用的时候直接设置值就可以了,键就是ThreadLocal本身 ...