问题描述:

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

解题思路:

这道问题还是比较简单的,首先要理解它的意思,整个容器的高度是由较短的那根线决定的。然后从两边分别开始往中间收缩,对短的那根线进行往里收缩。这是容器的高度是由短的那根线决定的,那么当对高线往中间收缩时候,面积是在逐渐减小的,所以应该对矮的那根线进行收缩。

代码如下:

public class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int maxArea = (right - left) * Math.min(height[left], height[right]);
int temp; while(left < right){
if(height[left] < height[right]){
left++;
}
else{
right--;
}
temp = (right - left) * Math.min(height[left], height[right]);
if(temp > maxArea)
maxArea = temp;
}
return maxArea;
}
}

Java [leetcode 11] Container With Most Water的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  4. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  6. 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]

    题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  7. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  9. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

随机推荐

  1. Xubuntu下Mentohust认证(校园网用户)

    对于路由器上网到用户来说,自动分配IP上网。 对于校园网用户,首先下载mentohust_0.3.4-1_i386.deb,双击安装程序 然后在命令窗口中输入sudo -s 密码:user来获得roo ...

  2. Xcode免证书打包ipa

    1,创建证书 打开“钥匙串访问”创建证书 填写好内容后点击继续,之后的步骤什么都不用改,一路点击“确定”和“继续”,最后完成这个向导就可以了. 我们创建的证书是不被信任的,右键点击证书选择“显示简介” ...

  3. OO之装饰者模式

    以下为装饰者模式详解: 引子: 假如有一个快餐店,基本种类分为米饭,水饺,粉面等,但每一种类型的快餐又可以搭配不同的料,如米饭可以点各种不同的菜(排骨,青菜,土豆等),如果按照一般的设计,快餐为基类, ...

  4. 使用MyBatis的resultMap高级查询时常用的方式总结

    以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...

  5. Netty 4.0 demo

    netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的nio网络框架 netty4.x和 ...

  6. python解决汉诺塔问题

    今天刚刚在博客园安家,不知道写点什么,前两天刚刚学习完python 所以就用python写了一下汉诺塔算法,感觉还行拿出来分享一下 首先看一下描述: from :http://baike.baidu. ...

  7. s3c2440之cache

    cache高速缓冲存储器注意与块设备页高速缓存进行区别,一个是硬件的实现一个是软件的实现,块设备页高速缓存. s3c2440/s3c2410里面主要有一个arm920t的核,但同时包含几个协处理器,协 ...

  8. execution 表达式

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) th ...

  9. python 记录日志logging

    在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...

  10. vncserver 添加用户

    1.在vncserver设置登录用户的信息   #vi /etc/sysconfig/vncservers   VNCSERVERS="1:root 2:wt" 此处添加用户   ...