LeetCode OJ-- Maximal Rectangle ***@
https://oj.leetcode.com/problems/maximal-rectangle/
给一个二维矩阵,里面只有0 1,求一个最大的矩阵,里面的所有元素都是1.
首先预处理: 0 1 1 1 0 1 1
做一个二维数组,记录到目前为止本行上连续1的个数,为 0 1 2 3 0 1 2
之后再对每一个位置进行遍历,从行方向向列延伸,记录这个过程中的最大面积。
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.empty() || matrix.size() == || matrix[].size() == )
return ;
int rows = matrix.size();
int cols = matrix[].size();
vector<vector<int> > row_ones;
row_ones.resize(rows);
for(int i = ; i<rows; i++)
row_ones[i].resize(cols);
for(int y = ; y<rows;y++)
{
int count = ;
for(int x = cols - ; x>=; x--)
{
if(matrix[y][x] == '')
count++;
else
count = ;
row_ones[y][x] = count;
}
}
int i_max = ;
for(int y = ; y < rows; y++)
{
for(int x = ; x < cols; x++)
{
if(row_ones[y][x] > )
{
for(int x_length = ; x_length <= row_ones[y][x]; x_length++)
{
int y_length = ;
while(y + y_length < rows && row_ones[y+y_length][x] >= x_length)
y_length++;
int m2 = y_length * x_length;
i_max = max(m2,i_max);
}
}
}
}
return i_max;
}
};
LeetCode OJ-- Maximal Rectangle ***@的更多相关文章
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode OJ] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- LeetCode OJ 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java for LeetCode 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- IP代理池之验证是否有效
IP代理池之验证是否有效 把proxy pool项目跑起来,但也不知道这些ip怎么用,爬虫的时候是否用代理去爬取,下面通过一个例子来看看. 代码如下: import requests PROXY_PO ...
- astyle 使用说明 —— 集成到开发平台中
转自:https://www.cnblogs.com/jiangxinnju/p/4908575.html 欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jian ...
- V4L2学习(一)整体说明
1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡是内核中的子系统都有抽象底层硬件的差异,为上层提供统一的接口和提取出公共代 ...
- 菜鸟学Linux - 用户与用户组基础
/etc/passwd: 用户的信息是保存在/etc/passwd下面(早期的时候,用户的密码也是放在该文件中.后来出于安全考虑,将密码放在/etc/shadow中去): /etc/group: 用户 ...
- 线段树[To be continued]
目录 数据结构--线段树 一.定义 二.性质 三.基本操作 0.结构体 1.建树 2.单点查询 3.单点修改 4.区间修改 5.区间查询 四.题目 单点修改.区域查询模板 五.鸣谢 学姐的Blog 百 ...
- Fragment 和 Activity 之间通信
在 Activity 中获取 Fragment 实例: FragmentManager 提供了一个类似于 findViewById 的方法,专门用于从布局文件中获取 Fragment 实例: //通过 ...
- python 学习分享-装饰器篇
本篇内容为偷窃的~哈哈,借用一下,我就是放在自己这里好看. 引用地址:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 第一步: ...
- md5 加密算法和升级
在这里插一小节加密的吧,使用openssl库进行加密. 使用MD5加密 我们以一个字符串为例,新建一个文件filename.txt,在文件内写入hello ,然后在Linux下可以使用命令md5sum ...
- Python面向对象之什么是类(1)
1.C#.Java :只能用面向对象编程 Ruby.Python :函数编程+ 面向对象 面向对象编程不是在所有地方都比函数式编程方便的,类是为了封装,下面是简单的使用方法 在创建类的时候要用clas ...
- maven学习(四)——maven项目构建过程
一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立Maven约定的目录结构和pom.xml文件 Hello | --src | -----main | ----- ...