Maximal Square
来源:互联网 发布:各国工资数据 编辑:程序博客网 时间:2024/06/13 07:32
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
For example, given the following matrix:
1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
public class Solution { public int maximalSquare(char[][] matrix) { if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; int m = matrix.length; int n = matrix[0].length; int[][] dp = new int[m][n]; int maxlen = 0; // init row; for(int i=0; i<n; i++){ if(matrix[0][i] == '1'){ dp[0][i] = 1; maxlen = 1; } } // init col; for(int i=0; i<m; i++){ if(matrix[i][0] == '1'){ dp[i][0] = 1; maxlen = 1; } } // calculate matrix; for(int i=1; i<m; i++){ for(int j=1; j<n; j++){ if(matrix[i][j] == '0'){ dp[i][j] = 0; } else { dp[i][j] = Math.min(dp[i-1][j-1], Math.min( dp[i][j-1], dp[i-1][j]))+1; maxlen = Math.max(maxlen, dp[i][j]); } } } return maxlen*maxlen; }}
0 0
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Php服务器部署问题之http错误404.3-Not Found
- Brothers 纪中4764 模拟
- Crisis 纪中 4765 树形dp
- priority_queue详解
- 友元类(Friend)是啥?
- Maximal Square
- 关于凸优化的一些简单概念
- CODEVS 1149 立体图
- android网络应用入门demo——Socket方式与获得服务器数据
- .NET转JAVA之路001
- Another Crisis_uva12186_树形dp
- 在bitbucket用git 用法
- Brothers_纪中4764_模拟
- 记录学习java的笔记