当前位置:网站首页>力扣——11.盛最多水的容器
力扣——11.盛最多水的容器
2022-07-30 12:06:00 【weixin_54096215】
1.题目


2.思路
首先判断这是一个长*高计算最大面积max的问题
1.需要两个指针,一个放在最左边,一个放在最右边,即i=0,j=height.length()-1;
2.判断最佳高度,max_height=if(height[i]<height[j]) ?i++ : j--;注意:是(height[i++])
3.算出面积,area=(max_height*(j-i+1))
4.判断一开始设定的max=0与area大小,利用Math.max(max,area);
自己的思路:一开始没有想到是计算最大面积的问题,而且用了两个循环来表示i,j,思路错误。
3.代码
class Solution {
public int maxArea(int[] height) {
int max = 0;
for(int i = 0,j= height.length-1;i<j; ){
int minHeight = height[i] < height[j] ? height[i++] : height[j--];
int area = (j - i + 1) * minHeight;
max = Math.max(max,area);
}
return max;
}
}边栏推荐
- Rust from entry to proficient 02-installation
- A tutorial on how to build a php environment under win
- Execution order of select, from, join, on where groupby, etc. in MySQL
- 反转链表-迭代反转法
- 13-GuliMall Basics Summary
- 什么是驱动程序签名,驱动程序如何获取数字签名?
- C# 枚举类型 于xaml 中区别
- Matlab基础(2)——向量与多项式
- 概率论得学习和整理6:概率的分布
- 数据湖(十八):Flink与Iceberg整合SQL API操作
猜你喜欢

作业7.29 目录相关函数和文件属性相关函数

备战金九银十!2022面试必刷大厂架构面试真题汇总+阿里七面面经+架构师简历模板分享

【记一个kaggle划水比赛】PetFinder.my - Pawpularity Contest 宠物预测

unity初学6——简易的UI制作(血条制作)和音频加入以及NPC的对话气泡(2d)

概率论的学习整理2:如何对随机实验的对象:“事件” 进行计数呢? 四种计数方法,不只是排列组合

OneNote如何修改已有的笔记本为默认的快速笔记?

C# 枚举类型 于xaml 中区别

mapbox-gl开发教程(十四):画圆技巧

Rust 从入门到精通02-安装

Difference between C# enumeration type and xaml
随机推荐
13-GuliMall 基础篇总结
【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解
微信视频号视频如何下载提取?视频号直播回放如何下载?方法很简单!
Underwater target detection method based on spatial feature selection
Digital input and output module DAM-5088
Matlab绘图(1)——二维绘图
Matlab基础(5)——符号运算
2022-07-29 顾宇佳 学习笔记 异常处理
The method of judging the same variable without the if branch
概率论的学习整理1: 集合和事件
【Kaggle比赛常用trick】K折交叉验证、TTA
概率论的学习整理--番外2:和二项式,组合相关的杨辉三角
打破原则引入SQL,MongoDB到底想要干啥???
维护数千规模MySQL实例,数据库灾备体系构建指南
shell的理解
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
Verilog grammar basics HDL Bits training 07
Execution order of select, from, join, on where groupby, etc. in MySQL
Program environment and preprocessing (detailed)
ModelCoder状态机:对柴油机工况判断策略进行建模