当前位置:网站首页>力扣(LeetCode)221. 最大正方形(2022.08.09)
力扣(LeetCode)221. 最大正方形(2022.08.09)
2022-08-10 06:08:00 【ChaoYue_miku】
在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。
示例 1:
输入:matrix = [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]]
输出:4
示例 2:
输入:matrix = [[“0”,“1”],[“1”,“0”]]
输出:1
示例 3:
输入:matrix = [[“0”]]
输出:0
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 300
matrix[i][j] 为 ‘0’ 或 ‘1’
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximal-square
C++提交内容:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return 0;
}
int maxSide = 0;
int rows = matrix.size(), columns = matrix[0].size();
vector<vector<int>> dp(rows, vector<int>(columns));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (matrix[i][j] == '1') {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
}
maxSide = max(maxSide, dp[i][j]);
}
}
}
int maxSquare = maxSide * maxSide;
return maxSquare;
}
};
边栏推荐
猜你喜欢
随机推荐
交换机的功能和ipv4
JS中初始化对象为null和空对象的区别
网页安全证书错误但无法安装证书的解决办法
Qt列表下方增加弹出加载数据提示效果
COLMAP+OpenMVS realizes 3D reconstruction mesh model of objects
I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
OpenGL学习笔记(LearnOpenGL)-第六部分 变换
UE 游戏模式
几行代码就可以把系统高崩溃;
第11章 数据库的设计规范【2.索引及调优篇】【MySQL高级】
VS Code插件国际化
强化学习_03_表格方法实践(CartPole-v0 And MontoCarlo)
Basic use of Log4j2
程序员的十楼层。看看自己在第几层。PS:我的目标是:30岁第四层
XV6 swtch.S详解
tqdm高级使用方法(类keras进度条)
指纹浏览器在使用易路代理时常见的问题及解决办法
数据库学习之数据类型
Qt借助隐藏控件和QSS绘制重复元素