221. Maximal Square
来源:互联网 发布:济南市社保查询软件 编辑:程序博客网 时间:2024/05/21 14:28
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.
Subscribe to see which companies asked this question
更好的方法:每个点填以这个点为左上角的最大方格。当一个点本身为1,同时下右和下右三个点都至少为n时,这个点给n+1
public class Solution { private int maxLen(char[][] m,int l,int i,int j){//判断当前点最大方格,至少边长l for(int x = i;x<i+l;x++){ for(int y = j; y< j+l;y++){ if(m[x][y]=='0')return 0; } } while(i+l<m.length&&j+l<m[0].length){ for(int x = i;x<=i+l;x++){ if(m[x][j+l]=='0')return l; } for(int y = j;y<=j+l;y++){ if(m[j+l][y]=='0')return l; } l++; } return l; } public int maximalSquare(char[][] matrix) { int hang = matrix.length; if (hang==0)return 0; int lie = matrix[0].length; int maxl = 0; // for(int i = 0;i<hang - maxl;i++){ // for(int j =0;j< lie - maxl;j++){ // } // } for(int i = hang-1;i>=0;i--){ for(int j = lie-1-maxl;j>=0;j--){ if(matrix[i][j]=='1'){ int a = maxLen(matrix,maxl+1,i,j); if(a>maxl)maxl=a; if(i>hang-1-maxl){i=hang-maxl-1;j=lie-maxl;if(i<0)break;}//下一轮进j=lie-maxl-1 } } } return maxl*maxl; }}
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
- 150. Evaluate Reverse Polish Notation
- C#/IOS/Android通用加密解密方法
- 【Hibernate步步为营】--多对多映射详解
- static的描述
- 对Android中集成支付宝支付流程的总结
- 221. Maximal Square
- 实现一个clone函数
- 【Hibernate步步为营】--关联映射之多对一
- 校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛(Problem E)
- 4444: [Scoi2015]国旗计划|贪心|倍增
- Java中父类与子类的普通代码块,构造代码块,静态代码块 初始化
- Effective Java第29条--将局部变量的作用域最小化
- android 高效开发工具
- 93. Restore IP Addresses