[Medium]Maximal Square
来源:互联网 发布:yyf淘宝店地址 编辑:程序博客网 时间:2024/05/22 11:44
问题:
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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
解法:
源码:
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int n = matrix.size(); if (n == 0) return 0; int m = matrix[0].size(); int f[n][m]; memset(f, 0, sizeof(f)); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == 0 || j == 0) { f[i][j] = matrix[i][j] - '0'; } else { f[i][j] = matrix[i][j] - '0'; if (f[i][j]) f[i][j] = min(min(f[i-1][j], f[i][j-1]), f[i-1][j-1]) + 1; } ans = max(ans, f[i][j]); } } return ans * ans; }};
0 0
- 【Medium】221. Maximal Square
- [Medium]Maximal Square
- 221. Maximal Square -Medium
- [Leetcode 221, Medium] Maximal square
- Medium 221题 Maximal Square
- Leetcode 221. Maximal Square (Medium) (cpp)
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- Maximal Square
- [Medium]Ugly Number II
- struts2的声明式异常处理
- Linux中Mongodb的使用
- POJ-3278-Catch That Cow[基础BFS]
- 修改ramdisk根文件系统方法
- [Medium]Maximal Square
- How tomcat works——3 连接器
- [Medium]Maximum Product Subarray
- [Easy]Best Time to Buy and Sell Stock
- 紫书 例题7-5 UVA 129
- [Medium]Triangle
- 集合之Collection、List
- HTTP状态码详解
- 夕拾算法初级篇:4)日期差值