leetcode138container-with-water
题目描述
注意:你不能倾斜容器
输出: 49
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
输出
49
class Solution {
public:
/**
*
* @param height int整型vector
* @return int整型
*/
int maxArea(vector<int>& height) {
// write code here
if (height.size()<2)//数据太少不能构成容器
return 0;
int i=0,j=height.size()-1;
int maxArea=0;
int tmpArea=0;
while (i<j){
tmpArea=min(height[i],height[j])*(j-i);
if (tmpArea>maxArea)
maxArea=tmpArea;
if (height[i]<height[j])
++i;
else
--j;
}
return maxArea;
}
};
class Solution {
public:
/**
*
* @param height int整型vector
* @return int整型
*/
int maxArea(vector<int>& height) {
// write code here
int l,r,Max=-9999;
for (l=0,r=height.size()-1;l<r;){
Max=max(Max,(r-l)*min(height[l],height[r]));
height[l]<height[r]?l++:r--;
}
return Max;
}
};
import java.util.*;
public class Solution {
/**
*
* @param height int整型一维数组
* @return int整型
*/
public int maxArea (int[] height) {
// write code here
if (height.length<2){
return 0;
}
int left=0;
int right=height.length-1;
int maxV=0;
while (left<right){
int v=Math.min(height[left],height[right])*(right-left);
maxV=Math.max(v,maxV);
if (height[left]<height[right]){
left++;
}else {
right--;
}
}
return maxV;
}
}
#
#
# @param height int整型一维数组
# @return int整型
#
class Solution:
def maxArea(self , height ):
# write code here
left=0
right=len(height)-1
V=0
while left<right:
V=max(V,min(height[left],height[right])*(right-left))
if height[left]<height[right]:
left+=1
else:
right-=1
return V
leetcode138container-with-water的更多相关文章
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 【leetcode】Container With Most Water
题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- STM32之旅5——IWDG
STM32之旅5--IWDG stm32有两个看门狗,一个独立看门狗(IWDG).一个窗口看门狗(WWDG):独立看门狗是时钟源是内部的40kHz的低速时钟,即使主频出问题了,独立看门狗也不会受到影响 ...
- OpenCV计算机视觉学习(4)——图像平滑处理(均值滤波,高斯滤波,中值滤波,双边滤波)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice &q ...
- 【C++设计模式二】工厂模式
(1)定义3 简单工厂模式中,每新增一个具体产品,就需要修改工厂类内部的判断逻辑.为了不修改工厂类,遵循开闭原则,工厂方法模式中不再使用工厂类统一创建所有的具体产品,而是针对不同的产品设计了不同的工厂 ...
- 【小白学PyTorch】21 Keras的API详解(下)池化、Normalization层
文章来自微信公众号:[机器学习炼丹术].作者WX:cyx645016617. 参考目录: 目录 1 池化层 1.1 最大池化层 1.2 平均池化层 1.3 全局最大池化层 1.4 全局平均池化层 2 ...
- 【LWJGL3】LWJGL3的内存分配设计,第一篇,栈上分配
简介 LWJGL (Lightweight Java Game Library 3),是一个支持OpenGL,OpenAl,Opengl ES,Vulkan等的Java绑定库.<我的世界> ...
- hugo官方相关文档地址
+++ date="2020-10-17" title="hugo官方相关文档地址" tags=["hugo"] categories=[& ...
- centos8平台使用ab做压力测试
一,安装ab [root@blog ~]# yum install httpd-tools 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/archi ...
- Linux下批量kill掉进程
ps -ef|grep java|grep -v grep|cut -c 9-15|xargs kill -9 管道符"|"用来隔开两个命令,管道符左边命令的输出会作为管道符右边命 ...
- web功能测试
web功能测试基础: https://www.cnblogs.com/wz123/p/9680484.html
- JS错误写法[清除DOM]
前言 我现在总结一下我之前敲代码犯的错误,清除DOM元素,我们开始写代码吧! HTML <h1 style="font-size: 18px;font-weight: bold;col ...