思路:
dp。
实现:
1 class Solution 2 { 3 public: 4 int maximalSquare(vector<vector<char>>& matrix) 5 { 6 if (matrix.empty()) return 0; 7 int m = matrix.size(), n = matrix[0].size(); 8 vector<int> dp(m, 0); 9 int maxn = 0; 10 for (int i = 0; i < m; i++) 11 { 12 dp[i] = matrix[i][0] - '0'; 13 maxn = max(maxn, dp[i]); 14 } 15 for (int i = 1; i < n; i++) 16 { 17 int pre = dp[0]; 18 dp[0] = matrix[0][i] - '0'; 19 maxn = max(dp[0], maxn); 20 for (int j = 1; j < m; j++) 21 { 22 if (matrix[j][i] == '1') 23 { 24 pre = min(pre, min(dp[j - 1], dp[j])) + 1; 25 swap(dp[j], pre); 26 maxn = max(maxn, dp[j]); 27 } 28 else dp[j] = 0; 29 } 30 } 31 return maxn * maxn; 32 } 33 };