[LeetCode]Maximal Square
来源:互联网 发布:dnf梦幻装备源码 编辑:程序博客网 时间:2024/06/07 17:16
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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[i][j]=Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;//状态转换方程
public class Solution { public int maximalSquare(char[][] matrix) { if(matrix.length==0||matrix[0].length==0) return 0; int[][] dp=new int[matrix.length][matrix[0].length]; int re=0; for(int i=0;i<matrix.length;i++){ dp[i][0]=matrix[i][0]=='1'?1:0; re=Math.max(dp[i][0],re); } for(int i=0;i<matrix[0].length;i++){ dp[0][i]=matrix[0][i]=='1'?1:0; re=Math.max(dp[0][i],re); } for(int i=1;i<matrix.length;i++){ for(int j=1;j<matrix[0].length;j++){ if(matrix[i][j]=='0') dp[i][j]=0; else dp[i][j]=Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1; re=Math.max(re,dp[i][j]); } } return re*re; }}
阅读全文
1 0
- 【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
- leetCode(27):Maximal Square
- [leetCode] Maximal Square
- Leetcode 221 Maximal Square
- 解决unity导入模型时出现多层UV的情况
- 使用React的static方法实现同构以及同构的常见问题
- 《C和指针》6.18.1匹配字符
- 噪声强度(噪声功率) 噪声方差到底有什么关系? matlab中的awgn函数
- [NOIP模拟]小x分砖块
- [LeetCode]Maximal Square
- Linux下线程池的实现
- 2440移植Linux Kernel笔记(六)------剪裁Kernel
- 【Python】2“数据类型和变量”
- ActiveMQ学习笔记-入门教程
- qwb与小数 思路+代码
- 《On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima》-ICLR2017文章阅读
- Android中常用的动作
- nginx禁止非法域名解析访问网站