当前位置:网站首页>盛最多水得容器
盛最多水得容器
2022-06-10 17:15:00 【精英干员瑕光】
原版:

个人翻译版:
给定一个整数数组,进行枚举,索引是X轴,数值是Y轴,找出其中两个数圈起来面积最大为多少。
先上自己的想法和操作。
设计一个二层循环,设立一个最大面积s1=0,每个数都从自身开始向后遍历,找到最大面积替换s1,代码如下:
class Solution:
def maxArea(self, height: List[int]) -> int:
carry=0
for i in range(len(height)):
for j in range(i,len(height)):
cantake=(j-i)*min(height[i],height[j])
carry=max(cantake,carry)
return carry结果:失败,代码超时。
此双层循环相当于每一个数都遍历一次列表,工作量太大。
正确答案:
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
area = min(height[l], height[r]) * (r - l)
ans = max(ans, area)
if height[l] <= height[r]:
l += 1
else:
r -= 1
return ans
使用双指针,分别指向列表两端,向中间移动。使用while可以避免设置边界值,同时比起双重循环操作明显更加简洁。
边栏推荐
猜你喜欢

com.netflix.client.ClientException: Load balancer does not have available server for client: userser

华为matepad能成为你的笔记本电脑副屏?

if else的使用太简单?(看懂这篇你的逻辑会进一步提升)
![[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service](/img/93/d999239b28afb2d9a61e9aad27d2cd.png)
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service

well! One new star, please look over | elder martial brother and elder martial sister say

训练时添加进度条的库--tqdm

嘿!ONES 新星请看过来|师兄师姐说

Library for adding progress bar during training --tqdm

2022版IDEA图形界面GUI乱码解决方法超详细简单版

There is an urgent need to enrich the smart home product line. Can fluorite be crowded on the sweeping robot track?
随机推荐
MMdetection之build_optimizer模块解读
力扣 20. 有效的括号
mmdetection之dataset类解读
LeetCode 321. 拼接最大数***
Lifeifei: I am more like a scientist in physics than an engineer
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
绘制混淆矩阵
Talk about message oriented middleware (1) and AMQP
There is an urgent need to enrich the smart home product line. Can fluorite be crowded on the sweeping robot track?
掌握高性能计算前,我们先了解一下它的历史
js锚点定位可以扩展很多功能
苹果放大招!这件事干的太漂亮了……
Library for adding progress bar during training --tqdm
最新好文 | 基于因果推断的可解释对抗防御
一个WPF开发的打印对话框-PrintDialogX
How will you integrate into the $20trillion "project economy" in five years
你还在手写TS类型代码吗
线性移动棋
well! One new star, please look over | elder martial brother and elder martial sister say
Nacos configuration management