LeetCode221——Maximal Square
来源:互联网 发布:男友一夜 知乎 编辑:程序博客网 时间:2024/06/09 17:56
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 0Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
实现:
int maximalSquare(vector<vector<char>>& matrix) {
int width = 0;
for (int row = 0; row < matrix.size(); row++) {
if (row + width > matrix.size()) {
break;
}
for (int col = 0; col < matrix[row].size(); col++) {
if (col + width > matrix[row].size()) {
break;
}
int w = getWidth(matrix, row, col);
if (width < w) {
width = w;
}
}
}
return width*width;
}
int getWidth(vector<vector<char>>& matrix, int row, int col) {
int wd = 0;
while (row+wd < matrix.size() && col + wd < matrix[row].size()) {
for (int r = 0; r < wd+1; r++) {
if (matrix[row+r][col+wd] == '0') return wd;
if (matrix[row+wd][col+r] == '0') return wd;
}
wd++;
}
return wd;
}
- LeetCode221——Maximal Square
- LeetCode221:Maximal Square
- LeetCode221. Maximal Square
- LeetCode—221.Maximal Square
- Leetcode—221.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
- 代理模式
- Quartz2D-07.利用Quartz实现自定义截图效果
- CodeForces 85D Sum of Medians(线段树 + 离散化)
- perl应用:一些perl例(不断更新中ing........)
- Auto Layout
- LeetCode221——Maximal Square
- Provided id of the wrong type for class
- 上海各大商圈汇总
- Qt XXXr.obj : error LNK2001: 无法解析的外部符号 3 个无法解析的外部命令问题
- 获取手机wifi热点分配的ip地址列表
- Android 开源框架之AndroidAnnotations快速入门
- 建立一个win32窗口(用于开发游戏)
- 取(2堆)石子游戏(威佐夫博弈+hdu2177)
- Nginx之location