当前位置:网站首页>力扣——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;
}
}边栏推荐
- PanGu-Coder: 函数级的代码生成模型
- 【32. 图中的层次(图的广度优先遍历)】
- AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...
- Matlab基础(2)——向量与多项式
- Beijing, Shanghai and Guangzhou offline events丨The most unmissable technology gatherings at the end of the year are all gathered
- LinkedList与链表
- 概率论的学习整理4:全概率公式
- 备战金九银十!2022面试必刷大厂架构面试真题汇总+阿里七面面经+架构师简历模板分享
- [BJDCTF2020]Cookie is so stable-1|SSTI injection
- 超图iServer rest服务之最佳路径分析
猜你喜欢

使用百度EasyDL实现明厨亮灶厨师帽识别

解码Redis最易被忽视的CPU和内存占用高问题

别被隐私计算表象骗了 | 量子位智库报告(附下载)

Concepts of cloud-native applications and 15 characteristics of cloud-native applications

Redis master-slave replication

概率论的学习整理3: 概率的相关概念

概率论的学习整理--番外1:可重复且无次序的计数公式C(n+k-1,k) 的例题 : 同时丢3个骰子,会有多少种情况?答案不是216而是56!

Another blast!Ali's popular MySQL advanced collection is open source, reaching P7

2022-07-29 顾宇佳 学习笔记 异常处理

Rust from entry to proficient 02-installation
随机推荐
Concepts of cloud-native applications and 15 characteristics of cloud-native applications
SCM engineers written questions induction summary
PanGu-Coder: 函数级的代码生成模型
IO/多路复用(select/poll/epoll)
contentDocument contentWindow,canvas 、svg,iframe
The method of judging the same variable without the if branch
作业7.29 目录相关函数和文件属性相关函数
Rust 从入门到精通02-安装
13-GuliMall 基础篇总结
Underwater target detection method based on spatial feature selection
【ASP.NET Core】选项类的依赖注入
PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化
重建丢失的数据
Add the device library after Vivado installation
[ASP.NET Core] Dependency Injection for Option Classes
Digital input and output module DAM-5088
和数集团:让智慧城市更智慧,让现实生活更美好
使用百度EasyDL实现明厨亮灶厨师帽识别
打破原则引入SQL,MongoDB到底想要干啥???
概率论的学习整理--番外2:和二项式,组合相关的杨辉三角