NC108. 最大正方形
描述
示例1
输入:
[[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]]
输出:
4
示例2
输入:
[[1,0,0],[0,0,0],[0,0,0]]
输出:
1
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-10-01
class Solution { public: /** * 最大正方形 * @param matrix char字符型vector<vector<>> * @return int整型 */ int solve(vector<vector<char> >& matrix) { // write code here int max_side=0, m=matrix.size(), n=matrix[0].size(); int x00, x01, x10; for(int i=1; i<m; i++){ for(int j=1; j<n; j++){ if(matrix[i][j]=='0') continue; x00 = matrix[i-1][j-1]-'0'; x01 = matrix[i-1][j]-'0'; x10 = matrix[i][j-1]-'0'; if(x00>0 && x01>0 && x10>0){ matrix[i][j] = '1' + min(x00,min(x01,x10)); } max_side = max(max_side, matrix[i][j]-'0'); } } return max_side * max_side; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 400KB, 提交时间: 2021-11-20
class Solution { public: /** * 最大正方形 * @param matrix char字符型vector<vector<>> * @return int整型 */ int solve(vector<vector<char> >& matrix) { // write code here if (matrix.size() == 0 || matrix[0].size() == 0) return 0; int m = matrix.size(), n = matrix[0].size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); int edge = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (matrix[i - 1][j - 1] == '1') { dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1; } edge = max(edge, dp[i][j]); } } return edge * edge; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 412KB, 提交时间: 2022-02-08
class Solution { public: int solve(vector<vector<char> >& matrix) { int size=matrix.size(); if(!size) return 0; unique_ptr<unique_ptr<int[]>[]> arr(new unique_ptr<int[]>[size]); for(int i=0;i<size;++i){ arr[i].reset(new int[size]()); } int result=0; if(matrix[0][0]=='1') result=arr[0][0]=1; for(int i=1;i<size;++i) if(matrix[i][0]=='1') result=arr[i][0]=1; for(int i=1;i<size;++i) if(matrix[0][i]=='1') result=arr[0][i]=1; for(int i=1;i<size;++i) for(int j=1;j<size;++j) if(matrix[i][j]=='1'){ arr[i][j]=min(arr[i-1][j-1],min(arr[i-1][j],arr[i][j-1]))+1; if(arr[i][j]>result) result=arr[i][j]; } return result*result; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 432KB, 提交时间: 2021-10-24
class Solution { public: /** * 最大正方形 * @param matrix char字符型vector<vector<>> * @return int整型 */ int solve(vector<vector<char> >& matrix) { // write code here if(matrix.size()==0) return 0; vector<vector<int>> dp(matrix.size()+1,vector<int>(matrix[0].size()+1,0)); int maxSide = 0; for(int i=1;i<=matrix.size();i++){ for(int j=1;j<=matrix[0].size();j++){ if(matrix[i-1][j-1]=='1'){ dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1])) + 1; maxSide = max(dp[i][j],maxSide); } } } return maxSide * maxSide; } };
C 解法, 执行用时: 3ms, 内存消耗: 312KB, 提交时间: 2021-10-30
/** * 最大正方形 * @param matrix char字符型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型 */ int min(int a, int b) { return (a<b) ? a : b; } int solve(char** matrix, int matrixRowLen, int* matrixColLen ) { // write code here int dp[matrixRowLen+1][*matrixColLen+1]; int maxNum = 0; for (int i=0; i<matrixRowLen+1; i++) { dp[i][0] = 0; } for (int i=1; i<*matrixColLen+1; i++) { dp[0][i] = 0; } for (size_t i = 1; i < matrixRowLen+1; i++) { for (size_t j = 1; j < *matrixColLen+1; j++) { if (matrix[i-1][j-1] == '0') { dp[i][j] = 0; } else { dp[i][j] = min(dp[i][j-1], min(dp[i-1][j-1], dp[i-1][j])) + 1; maxNum = (maxNum < dp[i][j]) ? dp[i][j] : maxNum; } } } return maxNum*maxNum; }