-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path221.最大正方形.cpp
43 lines (38 loc) · 963 Bytes
/
221.最大正方形.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* @lc app=leetcode.cn id=221 lang=cpp
*
* [221] 最大正方形
*/
// @lc code=start
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m==0)return 0;
int n = matrix[0].size();
if(n==0)return 0;
int maxSide = 0;
vector<vector<int>> dp(m, vector<int>(n, 0));
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(matrix[i][j]=='1'){
if(i==0 || j==0)
{
dp[i][j] = 1;
}else{
dp[i][j] = min(dp[i-1][j], min(dp[i-1][j-1], dp[i][j-1])) + 1;
}
maxSide = max(maxSide, dp[i][j]);
}
}
}
return maxSide * maxSide;
}
};
// @lc code=end