题目说明

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

For example, given the following matrix:

1 0 1 0 0

1 0 1 1 1

1 1 1 1 1

1 0 0 1 0

Return 4.


题目分析

我采用的方法比较笨拙,就是对于矩阵中的每一个元素,以反“L”型不断向右下扩增,以确定最大正方形的面积。

以下为个人实现(C++,25ms):

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int row = matrix.size();
int max_a = 0;
for (int i = 0; i < row; i++) {
int col = matrix[i].size();
for (int j = 0; j < col; j++) {
if (matrix[i][j] - '0') {
int a;
for (a = 1; i + a < row && j + a < col; a++) { // overflow judge
int off_x, off_y;
// go through in 'L' shape
for (off_x = 0; off_x < a + 1 && matrix[i + a][j + off_x] - '0'; off_x++);
if (off_x < a + 1) {
break;
}
for (off_y = 0; off_y < a + 1 && matrix[i + off_y][j + a] - '0'; off_y++);
if (off_y < a + 1) {
break;
}
} if (a > max_a) {
max_a = a;
}
}
}
}
return max_a * max_a;
}
};

毫无疑问这种解决方式太慢了。实际上我对很多点进行了重复判断,比如一个大正方形内部包含的若干小真方形,我都用“L”扩增的方式进行了判断,这种情况应该使用DP解决。

DP实现代码(C++,原贴):

int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty()) return 0;
int m = matrix.size(), n = matrix[0].size();
vector<int> dp(m + 1, 0);
int maxsize = 0, pre = 0;
for (int j = 0; j < n; j++) {
for (int i = 1; i <= m; i++) {
int temp = dp[i];
if (matrix[i - 1][j] == '1') {
dp[i] = min(dp[i], min(dp[i - 1], pre)) + 1;
maxsize = max(maxsize, dp[i]);
}
else dp[i] = 0;
pre = temp;
}
}
return maxsize * maxsize;
}

如果有一个边长为n(n>=2)的正方形,那么它的内部一定存在4个边长为n-1的正方形。于是代码实现的思路是如果某元素值为1,那么取其左元素、上元素和左上元素中的边长最小值+1,得到的结果就可以作为该元素的边长,否则该元素边长为0。

LeetCode题解:(221) Maximal Square的更多相关文章

  1. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  2. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  3. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

  4. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  5. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  6. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. [LeetCode] 221. Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. Java for LeetCode 221 Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

随机推荐

  1. 20145209刘一阳《JAVA程序设计》第七周课堂测试

    第七周课堂测试 1.命令"CREATE DATABASE "用来创建一个数据库.(A) A .true B .false 2.以下不属于驱动的四种类型的是(C) A .JDBC-O ...

  2. Linux命令学习笔记1

    1.Linux命令学习 2.Mkdir /data       -创建文件夹 在/下创建文件夹 data 3.Cd               -目录切换 列如cd / 4.Touch /data/1 ...

  3. matplotlib雷达图

    用matplotlib画雷达图,网上流传的版本其实都是官网的一个例子.但是那个例子太复杂,而且它封装了几个类,让人难以一眼看出其本质. 我给出一个简单的解决方法,没有任何封装.作本文的原因,是为了回答 ...

  4. 【CQOI2014】数三角形

    题面 题解 考虑使用总数减去不合法的数量 首先将\(n, m\)都加上\(1\),将网格变成坐标系 总数即为\(\large\binom{n\times m}{3}\) 不合法的有三种情况: 三个点在 ...

  5. 模拟UNIX(linux)文件系统

    操作系统课程设计 一.实验内容 1. 题目:模拟UNIX(linux)文件系统                [问题描述] 在任一OS下,建立一个大文件,把它假象成一张盘,在其中实现一个简单的 模拟U ...

  6. CSS快速入门-鼠标悬浮(hover伪类)

    一.概述 hover伪类:在鼠标移到元素上时向此元素添加特殊的样式.比较普通的就是一个url,当你鼠标放上去后,会变颜色. 在现实的应用场景也非常之多.最常见的是网站的悬浮导航,当鼠标放到导航条上时, ...

  7. 我用 Python 爬取微信好友,最后发现一个大秘密

    前言 你身处的环境是什么样,你就会成为什么样的人.现在人们日常生活基本上离不开微信,但微信不单单是一个即时通讯软件,微信更像是虚拟的现实世界.你所处的朋友圈是怎么样,慢慢你的思想也会变的怎么样.最近在 ...

  8. UWP 使用HttpClient获取网页数据

    我的App自然灾害中,为了展示地震的各种信息,就需要从网页上获取地震源数据. 如图所示,我们需要展示 地震等级.地震发生时间.经纬度.震源深度.地震位置等信息. 那么,假设给了一个地震的源,中国地震台 ...

  9. 使用html2canvas将html标签转化为图片

    有些时候我们无法用常规的截图工具截取网页内容,这时可以尝试以下方法: 1.首先允许跨域,Chrome浏览器可以在快捷方式->属性->目标栏里添加: --disable-web-securi ...

  10. alibaba/fescar 阿里巴巴 开源 分布式事务中间件

    Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 示例:https://github.com/windwant/ ...