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, ...
随机推荐
- AD(Altium Designer)PCB布线中的“格式刷”,助力快速布局布线
摘要:在AD(Altium Designer)进行电路板布线时,孔丙火(微信公众号:孔丙火)经常会碰到电路中有相同功能的模块,比如2路相同的RS485通信电路.多路相同继电器输出电路.多路相同的输入电 ...
- 转C++了
积极响应"某王"的号召,联赛之后转C辣!(好吧,其实是它拿着一把西瓜刀顶在我背后逼我转的)
- 实验二 C2C实践
实验二 C2C实践 [实验目的] 掌握网上购物的基本流程和C2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商务应用软件 [知识准备] 本实验 ...
- 多测师讲解html _链接标签004_高级讲师肖sir
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>链 ...
- Javascript判断数据类型的五种方式及其特殊性
Javascript判断数据类型的五种方式及区别 @ 目录 typeof instanceof Object.prototype.toString isArray iisNaN ----------- ...
- 写了多年代码,你会 StackOverflow 吗
写了多年代码,你会 StackOverflow 吗 Intro 准备写一个傻逼代码的系列文章,怎么写 StackOverflow 的代码,怎么写死锁代码,怎么写一个把 CPU 跑满,怎么写一个 Out ...
- scrapy LinkExtractors
class scrapy.linkextractors.LinkExtractor Link Extractors 的目的很简单: 提取链接。 每个LinkExtractor有唯一的公共方法是 ext ...
- 【传递闭包】HDU 2157 How many ways??
UPD:现在才发现本题是个传递闭包 题目内容 春天到了,HDU校园里开满了花,姹紫嫣红,非常美丽. 葱头是个爱花的人,看着校花校草竞相开放,漫步校园,心情也变得舒畅. 为了多看看这迷人的校园,葱头决定 ...
- git tag的应用
一,git的tag是什么? tag就是给commit起一个容易记忆容易理解的名字 说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectfores ...
- ansible用authorized_key模块批量推送密钥到受控主机(免密登录)(ansible2.9.5)
一,ansible的authorized_key模块的用途 用来配置密钥实现免密登录: ansible所在的主控机生成密钥后,如何把公钥上传到受控端? 当然可以用ssh-copy-id命令逐台手动处理 ...