11. Container With Most Water(头尾双指针)
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.
思路:每次移动矮的那侧的指针,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)
- int maxArea(int* height, int heightSize) {
- int start = ;
- int end = heightSize-;
- int ret = ;
- int maxStart = ;
- int maxEnd = ;
- while(start < end){
- if(height[start] < height[end]){
- if(height[start]*(end-start) > ret) ret = height[start]*(end-start);
- while(start<end){
- start++;
- if(height[start] > maxStart){
- maxStart = height[start];
- break;
- }
- }
- }
- else{
- if(height[end]*(end-start) > ret) ret = height[end]*(end-start);
- while(start<end) {
- end--;
- if(height[end] > maxEnd){
- maxEnd = height[end];
- break;
- }
- }
- }
- }
- return ret;
- }
11. Container With Most Water(头尾双指针)的更多相关文章
- 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 Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 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: 找出数组中最大的数 ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 11. 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, ai). ...
随机推荐
- configparser 模块
import configparser #按字典方式操作 config = configparser.ConfigParser() #创建config 对象 #config对象test 等于 {'na ...
- VC中编译报错:error C2011: 'fd_set' : 'struct' type redefinition
这是头文件包含顺序的问题,原因与解决办法见下面代码的注释. /* 包含下面这两个头文件时,必须把winsock2.h放在前面 否则编译报错,N多的重定义错误:例如 error C2011: 'fd_s ...
- CSS样式学习-2
一.大小 ①width宽:height高. !注释:<a><span>无法使用该方法调整大小 控制元素的大小:宽高.下例是宽高分别100像素的div标签. <div st ...
- 机器学习入门-文本数据-构造Tf-idf词袋模型(词频和逆文档频率) 1.TfidfVectorizer(构造tf-idf词袋模型)
TF-idf模型:TF表示的是词频:即这个词在一篇文档中出现的频率 idf表示的是逆文档频率, 即log(文档的个数/1+出现该词的文档个数) 可以看出出现该词的文档个数越小,表示这个词越稀有,在这 ...
- setTimeout闭包常见问题
经常会遇到这样的问题,setTimeout按序输出循环数字,而不是最后输出同一个数字: 题目: for (var i = 0; i < 5; i++) { setTimeout(function ...
- LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的 ...
- jvm问题
问题: 1. 一台服务器,部署多个服务,请问,这多个服务,对应的是一个jvm,还是多个jvm? 2. 一个线程,从controller 到 service,到DAO,会调用多个方法,请问是 对应一个 ...
- HTTP、TCP、IP协议常见面试题
前言:在看面试题之前,先了解一下基本定义. HTTP.TCP.IP协议基本定义 HTTP: (HyperText Transport Protocol)是超文本传输协议的缩写,它用于传送WWW方式的数 ...
- C# 容器重用避免GC 的论证
var lst = new List<int>(); lst.Capacity = ; var cnt = lst.Count; var cp = lst.Capacity; ; i< ...
- How to Pronounce the 50 States
How to Pronounce the 50 States (1/4) Share Tweet Share Tagged With: Places The US state names can be ...