leetcode刷题11. 盛最多水的容器
做题连接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. 盛最多水的容器的更多相关文章
- LeetCode随缘刷题之盛最多水的容器
package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...
- C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- Leetcode题库——11.盛最多水的容器
@author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...
- 【LeetCode】11. 盛最多水的容器
题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- LeetCode 11. 盛最多水的容器(Container With Most Water)
题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- Leetcode 11.盛最多水的容器 By Python
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- LeetCode 11 - 盛最多水的容器 - [双指针暴力]
题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...
随机推荐
- 计算机网络之JSONP跨域
JSONP跨域实现原理 百度联想词跨域实现 一.JSONP跨域实现原理 1.Web页面使用<script>引入JS文件时不受同源策略的影响.准确的说,所有拥有src属性的标签都不受同源策略 ...
- asp.net core NLog将日志写到文件
1.安装Nlog包 Install-Package NLog.Extensions.Logging -Pre 2.在项目添加nlog.config文件 2.1.nlog.config <?xml ...
- Intellij IDEA 修改jsp 不能实时更新
Intellij IDEA 修改jsp 不能实时更新 1. 首先,output要指定到项目的webapp下,这样应该就可以实时更新了 2. 我的问题是这样设置之后,也不可以,原来是可以的,重装系统之后 ...
- Visual studio 2017添加引用时报错未能正确加载ReferenceManagerPackage包的解决方法
vs2017添加引用时报错未能正确加载“ReferenceManagerPackage”包. - AusonSir - 博客园https://www.cnblogs.com/-bao/p/674941 ...
- 清北学堂学习总结day3
小学知识总结 上午篇 •积性函数的卷积公式 (1)(f * g)( n ) = ∑(d|n) f( d ) x g ( n / d ) (2)代码实现 LL f[N], g[N], h[N]; voi ...
- ABP core学习之二 IIS部署.NET CORE
本文是关于IIS部署.NET CORE的总结,以后有碰到问题将陆续添加 IIS部署.NET CORE总结 一.服务器环境 首先确定自己项目的core版本,然后下载对应的包在服务器上安装 下载地址: h ...
- Nginx:Linux下安装Nginx与配置
准备目录 [root@sijizhen ~]# mkdir /usr/local/nginx [root@sijizhen ~]# cd /usr/local/nginx/ 下载 1.Nginx,在h ...
- Ubantu更新hostname & hosts
一.概述 Hostname 即主机名,一般存放在 /etc/hostname 中.而hosts则是本地域名解析文件,存放于 /etc/hosts. 二.测试 2.1 hostname musion@m ...
- Linux中Too many open files
1.ulimit –a open files一项就是默认的句柄数,最大为 65536 2.修改最大open files /etc/security/limits.conf文件中,加入以下配置: * s ...
- 微软推 Azure 机器学习工具:Algorithm Cheat Sheet
微软推 Azure 机器学习工具:Algorithm Cheat Sheet [日期:2015-05-15] 来源:CSDN 作者:Linux [字体:大 中 小] Azure Machine ...