Java实现 LeetCode 221 最大正方形
221. 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。
示例:
输入:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
输出: 4
PS:
当我们判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。这是定性的判断,那具体的最大正方形边长呢?
我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况是是它的上方,左方和左上方为右下角的正方形的大小都一样的,这样加上该点就可以构成一个更大的正方形。
但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加1了。
假设dpi表示以i,j为右下角的正方形的最大边长,则有 dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1 当然,如果这个点在原矩阵中本身就是0的话,那dp[i]肯定就是0了。

难搞哦~想了半天,才弄明白,为什么?(ง •_•)ง
class Solution {
public int maximalSquare(char[][] matrix) {
/**
dp[i][j]表示以第i行第j列为右下角所能构成的最大正方形边长, 则递推式为:
dp[i][j] = 1 + min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]);
**/
int m = matrix.length;
if(m < 1) return 0;
int n = matrix[0].length;
int max = 0;
int[][] dp = new int[m+1][n+1];
for(int i = 1; i <= m; ++i) {
for(int j = 1; j <= n; ++j) {
if(matrix[i-1][j-1] == '1') {
dp[i][j] = 1 + Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1]));
max = Math.max(max, dp[i][j]);
}
}
}
return max*max;
}
}
Java实现 LeetCode 221 最大正方形的更多相关文章
- LeetCode——221. 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 暴力法 ...
- [LeetCode] 221. 最大正方形(DP)
题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- leetcode 221. 最大正方形
题目描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 思路分析: 一道动态规划的题.由于是正方形,首先单一的‘1’即为最小的正方形,接下来需要考察其外围区域 ...
- LeetCode 221. 最大正方形(Maximal Square)
题目描述 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
随机推荐
- ArrayList扩容机制实探
ArrayList初始化 问题:执行以下代码后,这个list的列表大小(size)和容量(capacity)分别是多大? List<String> list = new ArrayList ...
- asp.net core + entity framework core 多数据库类型支持实战
根据微软官方文档的说法,有两种方法可以实现在一个app中同时适应多种不同类型的数据库,并且全部支持migrations操作.其一,使用两个dbcontext:其二,修改migration文件,添加特定 ...
- [whu1568]dp优化
http://acm.whu.edu.cn/land/problem/detail?problem_id=1568 思路:先将所有数分解,得到2,3,5,7的个数,转化为用这些2,3,5,7" ...
- 基于R语言的航空公司客户价值分析
分析航空公司现状 1.行业内竞争 民航的竞争除了三大航空公司之间的竞争之外,还将加入新崛起的各类小型航空公司.民营航空公司,甚至国外航空巨头.航空产品生产过剩,产品同质化特征愈加明显,于是航空公司从价 ...
- 接口测试/soapUI
忙过了2019年的下半年终于在2020年快上线了,~鞭炮噼啪过~ 项目技术架构:XML请求数据 -> JAVA (转换)-> JOSN请求数据 项目使用工具:soapUI/Jmeter,m ...
- 使用ultraISO制作U盘制作系统盘提醒:设备忙,请退出所有在运行的应用。
U盘很久没用,今天重装系统用ultraISO做系统盘提示如下: 本人解决方法如下:打开设备管理器,磁盘.看看磁盘是不是有200M的EFI没有格掉 因为磁盘管理没有权限格U盘,网上找个工具,我随手找个d ...
- 小程序externalClasses介绍
小程序externalClasses 1.介绍:我们在封装组件的时候,有时候需要对外暴露出class,可以由调用者来决定组件中一部分的样式,此时就需要使用它了 // components/dong/i ...
- .net core 3.1 使用nlog记录日志 NLog.Web.AspNetCore
背景 .net core 中已经集成了log的方法, 但是只能控制台输出不能写入文件等等. 常见第三方的的日志工具包括log4net, nlog等等, 本文介绍nlog 一. 引用程序集, nuget ...
- oracle删除会话
create procedure killsessionas --set serveroutput on; --in oracle sql developer this cannot be ignor ...
- COCO数据集提取特定多个类并在YOLO-V3上训练
先占个地方,有空再写 ` import os Dir = './coco_class_6/Annotations/val2014' ImageDir = './coco_class_6/images/ ...