[LeetCode]221. Maximal Square
来源:互联网 发布:淘宝卖家骗局大全 编辑:程序博客网 时间:2024/05/17 08:31
Problem Description
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area.
[https://leetcode.com/problems/maximal-square/]
思路
DP!!
用一个二维数组保存以(i,j)为右下角的最长边长,辣么,如果(i,j)是1,len(i,j)就是len(i-1,j),len(i,j-1),len(i-1,j-1)中的最小值+1、
状态转移方程就酱。。。
Code
package q221;public class Solution { public int maximalSquare(char[][] matrix) { int m=matrix.length; if(m<1) return 0; int n=matrix[0].length; if(n<0) return 0; int max=0; int[][] len=new int[m+1][n+1]; for(int i=0;i<m;i++){ if(matrix[i][0]=='1'){ len[i][0]=1; max=1; } else len[i][0]=0; } for(int i=0;i<n;i++){ if(matrix[0][i]=='1'){ len[0][i]=1; max=1; } else len[0][i]=0; } for(int i=1;i<m;i++){ for(int j=1;j<n;j++){ if(matrix[i][j]=='1'){ len[i][j]=Math.min(Math.min(len[i-1][j], len[i][j-1]), len[i-1][j-1])+1; max=Math.max(max, len[i][j]); } else{ len[i][j]=0; } } } return max*max; }// public static void main(String[] args) {//// }}
0 0
- [leetcode] 221.Maximal Square
- 【leetcode】221. Maximal Square
- [LeetCode]221. Maximal Square
- leetcode 221. Maximal Square
- 221. Maximal Square LeetCode
- LeetCode 221. Maximal Square
- LeetCode *** 221. Maximal Square
- LeetCode-221.Maximal Square
- leetcode.221. Maximal Square
- Leetcode 221. Maximal Square
- [leetcode]221. Maximal Square
- [LeetCode]--221. Maximal Square
- leetcode 221. Maximal Square
- Leetcode 221. Maximal Square
- Leetcode 221. Maximal Square
- [LeetCode]221. Maximal Square
- 【Leetcode】221. Maximal Square
- [leetcode]221. Maximal Square
- Android中shape中的属性大全
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- postgresql 9.3.1 编译安装
- centos网络配置实例
- C++第一次作业
- [LeetCode]221. Maximal Square
- 关于内存数据与 JSON
- c++第一次实验
- 谈谈如何在面试中发掘程序猿的核心竞争力zz
- Cygwin中vi编辑器方向键和Backspace键不好使的解决方法
- BIT2014级软件学院程序设计-17 迷宫
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 解决cvc-complex-type.2.4.a: Invalid content was found starting with element 'page-encoding'. ..问题
- int 和 chart 的区别