当前位置:网站首页>盛最多水得容器
盛最多水得容器
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可以避免设置边界值,同时比起双重循环操作明显更加简洁。
边栏推荐
- matplotlib plt. Specific usage of text() - labeling points in a drawing
- 小程序积分商城如何实现营销目的
- LeetCode 255. Verifying preorder traversal sequence binary search tree*
- Leetcode 875. 爱吃香蕉的珂珂
- Abbexa低样本量鸡溶菌酶 C (LYZ) ELISA 试剂盒
- 亟需丰富智能家居产品线,扫地机器人赛道上挤得下萤石吗?
- com. netflix. client. ClientException: Load balancer does not have available server for client: userser
- Abbexa 1,3-二棕榈素 CLIA 试剂盒解决方案
- 掌握高性能计算前,我们先了解一下它的历史
- IIS安装 部署网站
猜你喜欢
随机推荐
IIS installation and deployment web site
LeetCode 321. 拼接最大数***
模板_计算组合数
树、森林和二叉树的关系
numpy——记录
Leetcode String to integer(Atoi)
THE LOTTERY TICKET HYPOTHESIS: FINDING SPARSE, TRAINABLE NEURAL NETWORKS论文笔记
牛客网:表达式求值
Force buckle 20 Valid parentheses
单片机底层通信协议① —— 同步和异步、并行和串行、全双工和半双工以及单工、电平信号和差分信号
mmdetection之dataloader构建
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
Numpy - record
LeetCode 321. Maximum number of splices***
yml文件配置参数定义字典和列表
软考不通过能不能补考?解答来了
期货账户资金安全吗?
Daily question -1287 Elements that appear more than 25% in an ordered array
传统企业在进行信息化升级的过程中,如何做好信息化顶层设计
sense of security








