leetcode个人题解——#11 Container with most water
class Solution {
public:
int maxArea(vector<int>& height) {
int max = ;
int l = ;
int r = height.size()-;
while(l < r)
{
int h = min(height[l], height[r]);
int area = h * (r-l);
if (area > max)
max = area;
while (height[l] <= h && l < r) l++;
while (height[r] <= h && l < r) r--;
}
return max;
}
};
思路:先计算最宽的面积,当中间每一个容器高度小于当前两端最低点时,肯定是不符合条件的,因为宽也减少了,略过即可。
leetcode个人题解——#11 Container with most water的更多相关文章
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- leetcode个人题解——#5 Container with most water
class Solution { public: string longestPalindrome(string s) { int length = s.length(); ) return s; ; ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
随机推荐
- Oracle语句(一)之简单查询
1.查询数据表的所有列: select * from 表名; 程序员正常用法:select 列名,列名... form 表名; 2.起别名: select 列名 [AS 别名],列名 别名...fro ...
- Spring知识点小结(二)
一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源: 导入dbcp的jar包:dbcp+pool+connector 代码实现: ...
- php 当不确定用户输入的是浮点 还是整数 还是字符串时
$price = (floatval($price))?intval(floatval($price)*100)/100:0;
- MySQL必知必会 读书笔记三:检索数据和数据排序
检索数据 SELECT语句 它的用途是从一个或多个表中检索信息. 为了使用SELECT检索表数据,必须至少给出两条信息--想选择什 么,以及从什么地方选择. 检索单个列 SELECT col_1 FR ...
- 获取DOM
<template> <div> <header-vue :msg="msg" ref="header">heheh< ...
- Hive(5)-DDL数据定义
一. 创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_pat ...
- 通过samba服务将centos7指定文件挂载到window下
做嵌入式开发,windows下编辑代码,虚拟机上编译,为了方便打算在虚拟机下搭一个samba服务器,将文件夹映射到windows下,搜索网上的方法,内容大同小异,试了半天终于成功了.特此记录一下步骤, ...
- golang 正则表达式 匹配局域网
做一个微服务,需要对http头域里的remoteip做访问限制:所有局域网都要鉴权,其中一些特殊ip,如网关地址,直接拒绝,防止公网访问.正则表达式很好的解决了这个,直接贴代码,读者拿来直接改改就能用 ...
- AngularJS-Learning ui-router angular-transitions
https://github.com/mgechev/AngularJS-Learning https://github.com/angular-ui/ui-router https://github ...
- 滑雪_KEY
滑雪 ( skiing.pas/c/cpp) [题目描述] MM 参加一个滑雪比赛,滑雪场是一个 N×M 的矩形, MM 要从起点( 1, 1)滑到( N,M).矩形中每个单位格子有一个海拔高度值 h ...