做题连接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. python3 动态import

    有些情况下,需要动态的替换引入的包 1.常用的import方法 import platform import os 2.__import__ 动态引用 loop_manager = __import_ ...

  2. centos6 & centos 7 防火墙设置

    转载:原文地址:http://blog.csdn.net/u011846257/article/details/54707864 Centos升级到7之后,内置的防火墙已经从iptables变成了fi ...

  3. go语言圣经练习

    练习 3.10: 编写一个非递归版本的comma函数,使用bytes.Buffer代替字符串链接操作. package main import ( "fmt" "os&q ...

  4. Python实现身份证号码有效性验证

    # -*- coding: utf-8 -*- import re #Errors=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!' ...

  5. sql server 删除所有表和递归查询、数字类型转为字符串

    1.删除所有表 select 'drop table '+name+';' from sys.tables where name like 'DataSyncV1DelaySample%' or na ...

  6. Linux 文本处理工具记录

    Shuffle lines of multi files 现在有 1000 个文本文件(0.txt ~ 999.txt),每个文件大概 11M,总共 11G,我想把这 1000 个文本文件的内容随机组 ...

  7. 获取spring的IOC核心容器,并根据id获取对象

    public class Client { /** * 获取spring的IOC核心容器,并根据id获取对象 * ApplicationContext的三个常用实现类 * classPathXmlAp ...

  8. vivado中如何使用chipscope

    如何使用chipscope 参考: https://www.cnblogs.com/liujinggang/p/9813863.html Xilinx FPGA开发实用教程---徐文波 田耘 1.Ch ...

  9. 51nod1229 序列求和 V2

    这题...毒瘤吧,可能要写两份代码... 传送门 noteskey 我们考虑这里的复杂度肯定是与 k 相关的,而且平方也是没问题的,那么我们先看看 S(k) 能怎么得到: \[\begin{align ...

  10. spool_sqlldr

    --0.制表符chr(9) 换行符chr(10) 回车符chr(13) windows下批处理.bat linux下批处理.shset echo offset feedback offset page ...