leetcode之maximal square
来源:互联网 发布:音序器软件 编辑:程序博客网 时间:2024/06/05 16:24
题目:
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.
解答:
很明显的是一个dp,使用dp[i][j]表示以i,j为右下点的时候的最大的正方形的周长
dp[i][j] =min(min(dp[i - 1][j],dp[i][j - 1]),dp[i - 1][j - 1]);
class Solution {public: int getVal(int i,int j,vector<vector<int>> &dp) {if(i < 0 || i >= dp.size() || j < 0 || j >= dp[0].size())return 0;return dp[i][j]; } int maximalSquare(vector<vector<char>>& matrix) { int rows = matrix.size();if(!rows)return 0;int cols = matrix[0].size();if(!cols)return 0;vector<vector<int>> dp(rows,vector<int>(cols,0));int res = 0;for(int i = 0;i < rows;++i){for(int j = 0;j < cols;++j){if(matrix[i][j] == '0')dp[i][j] = 0;elsedp[i][j] = min(min(getVal(i - 1,j,dp),getVal(i, j - 1,dp)) , getVal(i - 1,j - 1,dp)) + 1;if(dp[i][j] > res)res = dp[i][j];}} return res * res; }};
0 0
- leetcode之Maximal Square
- leetcode之maximal square
- Leetcode学习之Maximal Square(221)
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- [leetcode] Maximal Square
- Maximal Square Leetcode Java
- [leetcode] Maximal Square
- leetcode 221: Maximal Square
- LeetCode Maximal Square
- Maximal Square - LeetCode 221
- #leetcode#Maximal Square
- LeetCode Maximal Square
- [LeetCode] Maximal Square
- [LeetCode]Maximal Square
- leetcode Maximal Square
- leetcode Maximal Square
- Leetcode 221 Maximal Square
- 如何在RowEditing中使用TextArea时回车可以换行
- Self Summary: Back Propagation (BP) network
- SVN笔记(1)
- 页面跳转
- block(代码块)的介绍以及使用方法和变量之间的关系
- leetcode之maximal square
- java基础知识总结3
- JDK安装与环境变量配置
- Xcode技巧:项目导入外部文件后编译运行出现Undefined symbols for architecture x86_64错误
- iOS开发笔记--UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
- javaScript——&
- view.bringtofront
- linux crontab定时执行任务
- 【BZOJ4034】[HAOI2015]T2【树链剖分】【线段树】