做题连接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. hive字段名、注释中文显示问号

    问题如下图: 解决方法: header1的/etc/my.conf文件,在[mysqld]分组下面添加配置:character-set-server=utf8init_connect='SET NAM ...

  2. Redis集群管理

    1.简介 Redis在生产环境中一般是通过集群的方式进行运行,Redis集群包括主从复制集群和数据分片集群两种类型. *主从复制集群提供高可用性,而数据分片集群提供负载均衡. *数据分片集群中能实现主 ...

  3. Promise库

    标准 https://promisesaplus.com/ An open standard for sound, interoperable JavaScript promises—by imple ...

  4. vue项目中遇到的那些事。

    前言 有好几天没更新文章了.这段实际忙着做了一个vue的项目,从 19 天前开始,到今天刚好 20 天,独立完成. 做vue项目做这个项目一方面能为工作做一些准备,一方面也精进一下技术. 技术栈:vu ...

  5. 20164305 徐广皓 Exp1+ 逆向进阶

    实验内容 Task1 (5-10分) 自己编写一个64位shellcode.参考shellcode指导. 自己编写一个有漏洞的64位C程序,功能类似我们实验1中的样例pwn1.使用自己编写的shell ...

  6. 问题:强制关闭Redis快照导致不能持久化

    运行Redis发生错误:"MISCONF Redis is configured to save RDB snapshots, but is currently not able to pe ...

  7. ‘Host’ is not allowed to connect to this mysql server

    ‘Host’ is not allowed to  connect to this mysql server mysql 数据库不允许远程连接 方法一:修改 host 表 进入mysql数据库,选择m ...

  8. python时间模块小结

    1.datetime 模块 为日期和时间处理同时提供了简单和复杂的方法.支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出.该模块还支持时区处理: 简单例子: from datetime ...

  9. git查日志命令

    git常用命令 1.创建仓库 a.当前目录创建仓库,即把当前目录的文件开始用git管理,该命令会在当前目录下创建一个.git目录 git init b.指定目录创建仓库 git init 目录名 2. ...

  10. Large-Margin Softmax Loss for Convolutional Neural Networks

    paper url: https://arxiv.org/pdf/1612.02295 year:2017 Introduction 交叉熵损失与softmax一起使用可以说是CNN中最常用的监督组件 ...