类型的引用:Solution *s=new Solution();

1.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.

Note: You may not slant the container and n is at least 2.

给定一个vector作为纵线的高度输入,选择两条纵线使得他们和X轴一起构成的容器能够盛更多水。

参考:http://blog.csdn.net/patkritlee/article/details/52453417

解析:假设最大容器两条纵线为i,j,那么:a.在j的右端没有一条线会比它高!

b.在i的左端也不会有比它高的线!

c.所以我们从两头向中间靠拢,同时更新候选值;在收拢区间的时候优先从x,y中较小的边开始收缩;

代码:

class Solution {
public:
    int maxArea(vector<int>& height) {
        int l=0;
    int r=height.size()-1;
    int area=0;
    while(l<r){
        area=max(area,(r-l)*min(height[r],height[l]));

//查找l,r之间较小的h,从较小的一端开始逼近,找中间比它高的数值。
        if(height[l]<height[r]){
            int k=l;
            while(height[k]<=height[l])
                k++;
            l=k;
        }
        else{
            int k=r;
            while(height[k]<=height[r])
                k--;
            r=k;
        }
    }
 
    return area;
    }
};

leetcode笔记--水箱问题的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

随机推荐

  1. HL7体系入门级介绍【转】

    HL7的简单介绍1)HL7  缩写于Health Level Seven,是创建于1987年,用来发展独立卫生保健行业的电子交换交换标准,经过多年的发展,HL7已经有多个版本,     目前我们 的集 ...

  2. 移动端根据不同DPR加载大小不同的图片

    1.首先创建mixin.styl文件代码如下: bg-image($url) // 创建bg-image($url)函数 background-image: url($url + "@2x. ...

  3. [转]Spring Boot修改最大上传文件限制:The field file exceeds its maximum permitted size of 1048576 bytes.

    来源:http://blog.csdn.net/awmw74520/article/details/70230591 SpringBoot做文件上传时出现了The field file exceeds ...

  4. 设置VMware10开机自启动并同时启动虚拟机镜像系统

    首先,进入VMware Workstation的安装目录 C:\Program Files (x86)\VMware\VMware Workstation

  5. 伪分布式hbase从0.94.11版本升级stable的1.4.9版本

    Hbase从0.94.11升级到stable的1.4.9版本: 升级思路: hadoop1.1.2    hbase 0.94.11                             ↓ had ...

  6. BZOJ3230 相似子串 字符串 SA ST表

    原文链接http://www.cnblogs.com/zhouzhendong/p/9033092.html 题目传送门 - BZOJ3230 题意 给定字符串$s$.长度为$n$. 现在有$Q$组询 ...

  7. Java中字符串比较的问题

    package com.hxl; import java.util.Scanner; public class Test { public static void main(String[] args ...

  8. javaNIO的总结

    放大1.5倍查看 使用NIO对文件进行COPY操作 public class TestNIOCopyFile { public static void main(String[] args) thro ...

  9. Snowflake Snow Snowflakes POJ - 3349(hash)

    You may have heard that no two snowflakes are alike. Your task is to write a program to determine wh ...

  10. 多媒体开发(8):调试FFmpeg

    编译FFmpeg得到二进制文件,之后就是对二进制库的调用,这时FFmpeg就像一个黑盒子.作为程序员,难道不想研究一下FFmpeg的具体实现?比如是怎么拿到歌曲信息的.怎么解码的.怎么推流的,等等. ...