0221.cpp (618B)
1 class Solution { 2 public: 3 int maximalSquare(vector<vector<char>> &matrix) { 4 int n = matrix.size(), m = matrix[0].size(), res = 0; 5 vector<vector<int>> dp(n, vector<int>(m, 0)); 6 for (int i = 0; i < n; i++) { 7 for (int j = 0; j < m; j++) { 8 if (!i || !j || matrix[i][j] == '0') { 9 dp[i][j] = matrix[i][j] - '0'; 10 } else { 11 dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1; 12 } 13 res = max(res, dp[i][j]); 14 } 15 } 16 return res * res; 17 } 18 };