LeetCode *** 221. Maximal Square
2016-04-24 18:04
381 查看
题目:
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:
Return 4.
分析:
代码:
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 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 m = matrix.size(); if (m<1)return 0; int n = matrix[0].size(), res = INT_MIN; int dp[m] ={0}; for (int i = 0; i<m; ++i) { for (int j = 0; j<n; ++j) { if (matrix[i][j] == '1') { if (i > 0 && j > 0) { if (matrix[i - 1][j - 1] == '1'&&matrix[i - 1][j] == '1'&&matrix[i][j - 1] == '1') { if (dp[i - 1][j - 1] == dp[i - 1][j] && dp[i - 1][j] == dp[i][j - 1]) dp[i][j] = (int)pow(sqrt(dp[i - 1][j - 1]) + 1, 2); else { int x = min(dp[i - 1][j - 1], dp[i - 1][j]); x = min(x, dp[i][j - 1]); dp[i][j] = (int)pow(sqrt(x) + 1, 2); } } else dp[i][j] = 1; } else dp[i][j] = 1; } else dp[i][j] = 0; res = max(dp[i][j], res); } } return res; } };
相关文章推荐
- iOS 对plist增改操作
- Terracotta收购Ehcache (转)
- Leetcode Increasing Triplet Subsequence
- c++中的cin,cin.get(),cin.getline()读取字符串
- AndroidStudio使用笔记
- 240. Search a 2D Matrix II
- git操作详解
- PV(访问量)、UV(独立访客)、IP(独立IP) (转)
- 使用gulp-sass 和 gulp-livereload 自动编译sass文件
- C++ primer
- Unity学习笔记四 - Survival Shooter Tutorial
- 心碎的Basic SQL&Assignment
- MyEclipse做的项目导入到Eclipse中错误
- spring aop的xml配置详解
- Android Framework Java层是如何拦截并分发底层传送来的按键事件
- Linux初认识(一)
- View生成Bitmap
- c中数据的存储(内存)
- 从零开始学动态规划
- eclipse开发velocity实例(初学)