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 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.
题目链接:https://leetcode.com/problems/maximal-square/。
这一题有点相似:LeetCode OJ 之 Maximal Rectangle (最大的矩形)。可是解题方法全然不同。
思路:
动态规划。设f[i][j]表示包含当前点的正方形的最大变长,有递推关系例如以下:
f[0][j] = matrix[0][j]
f[i][0] = matrix[i][0]
For i > 0 and j > 0:
if matrix[i][j] = 0, f[i][j] = 0;
if matrix[i][j] = 1, f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1.
代码1:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix)
{
int row = matrix.size();
if(row == 0)
return 0;
int col = matrix[0].size();
vector<vector<int> > f(row , vector<int>(col , 0));
int maxsize = 0; //最大边长
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
if(i == 0 || j == 0)
f[i][j] = matrix[i][j]-'0';
else
{
if(matrix[i][j] == '0')
f[i][j] = 0;
else
f[i][j] = min(min(f[i-1][j] , f[i][j-1]) , f[i-1][j-1]) + 1;
}
maxsize = max(maxsize , f[i][j]);
}
}
return maxsize * maxsize;
}
};
代码2:
优化空间为一维
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix)
{
int row = matrix.size();
if(row == 0)
return 0;
int col = matrix[0].size();
vector<int> f(col , 0);
int tmp1 = 0 , tmp2 = 0;
int maxsize = 0; //最大边长
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
tmp1 = f[j]; //tmp1把当前f[j]保存以下,用来做下一次推断f[i+1][j+1]的左上角f[i-1][j-1]
if(i == 0 || j == 0)
f[j] = matrix[i][j]-'0';
else
{
if(matrix[i][j] == '0')
f[j] = 0;
else
f[j] = min(min(f[j-1] , f[j]) , tmp2) + 1; //这里的tmp2即是代码1的f[i-1][j-1]
}
tmp2 = tmp1 ; //把tmp1赋给tmp2,用来下次for循环求f[j+1]
maxsize = max(maxsize , f[j]);
}
}
return maxsize * maxsize;
}
};LeetCode OJ 之 Maximal Square (最大的正方形)的更多相关文章
- 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 ret ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- 50.Maximal Square(最大正方形)
Level Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...
- 【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 ...
- LeetCode OJ:Maximal Rectangle(最大矩形)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode OJ 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 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
随机推荐
- js 实现继承的6种方式(逐渐优化)
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- LevelDB和ForestDB简单性能测试(含代码)
测试环境简单说明 Windows下测试 硬件环境如下: 处理器:Intel(R) Core(TM) i5-4460 CPU @ 3.20GHz 内 存:8GB 硬 盘:希捷 ST1000DM003 操 ...
- Linux和windows下内核socket优化项 (转)
问题: No buffer space available Linux: vi /etc/sysctl.conf net.core.netdev_max_backlog = 30000 每个网络接口 ...
- SpringBoot开发项目,引入JPA找不到findOne方法
写在前面 开发SpringBoot的DAO层之后,去测试的时候,发现findOne()这个方法找不到了,查看了对应的表字段名和实体类的属性都一致 网上有通过降低版本解决的, 方式太牵强. 还有一种方式 ...
- jeecg平台testDatagrid
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- boost boost::asio::read socket.read_some 区别
boost boost::asio::read 尝试读一定数量的字节,直到读到为止,或者出错 socket.read_some 读一下socket,读到多少算多少 带async的类似
- HDUOJ-----Climbing Worm
Climbing Worm Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 程序员,不要让自己做兔子(updated) 网上最近流传的一个笑话,关于兔子,狼还有一只老虎的,故事 我就是想打你了,还需要什么理由吗?谁让你是兔子 项目经理是这样当的
程序员,不要让自己做兔子(updated) 前段时间和一个朋友聊天,酒席间向我抱怨他那段时间的郁闷:项目经理从客户那里拿来一个需求,实际上就是一个ppt描述,我这个朋友拿过来看后刚开始不觉得什么,一个 ...
- web前端工程师在移动互联网时代里的地位问题 为啥C/S系统在PC端没有流行起来,却在移动互联网下流行了起来 为啥移动端的浏览器在很多应用里都是靠边站,人们更加倾向于先麻烦自己一下,下载安装个客户端APP
web前端工程师在移动互联网时代里的地位问题 支付宝十周年推出了一个新产品:支付宝的十年账单,我也赶个时髦查看了一下我的支付宝十年账单,哎,感慨自己真是太屌丝了,不过这只是说明我使用淘宝少了,当我大规 ...
- Agile Development敏捷软件开发之何为敏捷开发
敏捷软件开发之何为敏捷开发 敏捷开发,Agile Development,就是指能够在需求迅速变化的情况下快速开发软件.我们接触最多敏捷实践方式有:极限编程(XP).结对编程.测试驱动开发(TDD)等 ...