221. Maximal Square
来源:互联网 发布:重载 java 编辑:程序博客网 时间:2024/05/18 03:36
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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 0
Return 4.
题意:给出一个矩阵,用字符0,1填充,求1组成的最大的方格。
思路:画一个二维数组,遇到0对应值写0,遇到1,判断对角线以及左边和上边的值,如果均不为0,则该值等于周围最小的值加1。用dp[m][n]记录点(m, n)时,以点(m ,n)为右下角的方格的边长值,则以该点为右下角点的方格的边长值为dp[m][n] = min{dp[m-1][n-1], dp[m-1][n], dp[m][n-1]}+1。
class Solution {public:int maximalSquare(vector<vector<char>>& matrix) {int m = matrix.size();if (m == 0)return 0;int n = matrix[0].size();vector<vector<int>> dp(m, vector<int>(n, 0));int max = 0;for (int i = 0; i < m; i++){if (matrix[i][0] == '1')dp[i][0] = 1, max = 1;}for (int i = 0; i < n; i++){if (matrix[0][i] == '1')dp[0][i] = 1, max=1;}for (int i = 1; i < m; i++){for (int j = 1; j < n; j++){if (matrix[i][j] == '1'){dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]))+1;if (dp[i][j] > max)max = dp[i][j];}}}return max*max;}};
0 0
- 221.Maximal Square
- [leetcode] 221.Maximal Square
- 【leetcode】221. Maximal Square
- [LeetCode]221. Maximal Square
- leetcode 221. Maximal Square
- 221. Maximal Square LeetCode
- 221. Maximal Square
- 221. Maximal Square
- LeetCode 221. Maximal Square
- 221. Maximal Square
- LeetCode *** 221. Maximal Square
- 221. Maximal Square
- LeetCode-221.Maximal Square
- leetcode.221. Maximal Square
- 221. Maximal Square
- Leetcode 221. Maximal Square
- 221. Maximal Square
- 221. Maximal Square
- Linux下静态链接库与动态链接库的区别
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- 建表 相关
- Caffe错误:error C2220: 警告被视为错误 - 没有生成“object”文件
- 计算机会议排名等级
- 221. Maximal Square
- pat L3-003. 社交集群
- 4.4 IKEv2 Win7 客户端证书认证
- shell脚本中Sha-Bang(#!)解释
- 无法连接远程桌面--必须为远程桌面启用Windows防火墙例外
- LeetCode 2 : Add Two Numbers ---- 单链表相加
- Java字符串编码与解码
- IOS 检查版本更新
- 欢迎使用CSDN-markdown编辑器