做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/

本题分为两种方法:

暴力法:

int maxArea(int* height, int heightSize) {
     int maxarea=0,s;
     for(int i=0;i<heightSize;i++)
         for(int j=i+1;j<heightSize;j++)
         {
             s=(height[i]>height[j])?height[j]:height[i];
             maxarea=(s*(j-i)>maxarea)?(s*(j-i)):maxarea;
            
         }
     return maxarea;

}

分别比较数组中边缘最小,然后面积较大值返回

头尾指针法:

int maxArea(int* height, int heightSize) {
     int maxarea=0,s,i=0,j=heightSize-1;
         while(i<j){
            
             s=(height[i]>height[j])?height[j]:height[i];
             maxarea=(s*(j-i)>maxarea)?(s*(j-i)):maxarea;
             if(height[i]<height[j])
                 i++;
             else
                 j--;
            
         }
     return maxarea;

}

leetcode刷题11. 盛最多水的容器的更多相关文章

  1. LeetCode随缘刷题之盛最多水的容器

    package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...

  2. C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...

  3. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  4. 力扣Leetcode 11. 盛最多水的容器

    盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

  5. Leetcode题库——11.盛最多水的容器

    @author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...

  6. 【LeetCode】11. 盛最多水的容器

    题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...

  7. LeetCode 11. 盛最多水的容器(Container With Most Water)

    题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...

  8. Leetcode 11.盛最多水的容器 By Python

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  9. LeetCode 11 - 盛最多水的容器 - [双指针暴力]

    题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...

随机推荐

  1. hadoop记录-hadoop常用

    1.hdfs目录配额 #设置配额目录hdfs dfsadmin -setSpaceQuota 10T /user/hive/warehouser/tmp查看配额目录信息hdfs dfs -count ...

  2. checkbox 实现互斥选择

    // mutex 互斥 checkbox 互斥/** 互斥的原理.找到需要互斥的所有的元素.赋值 checked=false; 然后单独赋值 checked=true* */var mutexbox ...

  3. webpack打包理解

    webpack打包理解(将所有依赖文件打包到一个文件中) 由于前端代码变得越来越多,越来越复杂, 纯粹脚本化的代码书写方式已经不能满足工程化得需求. 前端模块被抽象出来, 不仅仅包括js模块, 其它如 ...

  4. ArcGIS Editor for Open Street Map 10.X for Desktop下载地址

    ArcGIS Editor for Open Street Map可用于导入从OSM下载的地图,但并不是ArcGIS自带的工具,需要从官网下载,虽然文件很小,但下载速度较慢,易断开. 在此为找不到或不 ...

  5. 初次接触scrapy框架

    初次接触这个框架,先订个小目标,抓取QQ首页,然后存入记事本. 安装框架(http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/install.html) ...

  6. centos环境下安装redis

    1.安装gcc和gcc-c++ 使用which gcc 和which g++检查是否已经安装,若已有安装,跳过下面安装步骤 安装gcc yum -y install gcc 安装gcc-c++ yum ...

  7. 查表法解决calendar中月份及星期初始值为0的情况。

    Calendar ca = Calendar.getInstance(); String [] index = {"星期一","星期二","星期三&q ...

  8. C++变量/函数命名规范

    ## 参照Google C++编程规范之变量命名 1. 变量 变量名一律小写,单词间以下划线相连.类的成员变量以下划线结尾. 普通变量命名 举例: string window_name; // OK ...

  9. Spring Cloud 2-Config 分布式配置中心(七)

    Spring Cloud  Config  1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...

  10. scrapy 爬取时很多重复 及日志输出

    日志输出参考:https://blog.csdn.net/weixin_41666747/article/details/82716688 首先 item 要设置循环外 第二,request 要设置下 ...