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 return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
解题思路:
dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长,JAVA实现如下:
public int maximalSquare(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return 0;
int res = 0;
int[] dp = new int[matrix[0].length];
for (int i = 0; i < matrix[0].length; i++)
if (matrix[0][i] == '1'){
dp[i] = 1;
res=1;
}
for (int i = 1; i < matrix.length; i++) {
dp[0] = matrix[i][0] == '1' ? 1 : 0;
res=Math.max(res, dp[0]);
for (int j = 1; j < matrix[0].length; j++) {
if(matrix[i][j]=='0')
dp[j]=0;
else if (dp[j] == dp[j - 1]
&& matrix[i - dp[j]][j - dp[j]] == '0') {
dp[j] = Math.min(dp[j], dp[j - 1]);
} else
dp[j] = Math.min(dp[j], dp[j - 1]) + 1;
res=Math.max(res, dp[j]);
}
}
return res*res;
}
Java for LeetCode 221 Maximal Square的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- (medium)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. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- Leetcode 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
随机推荐
- ubuntu系统下使用锐捷上网的方法——特快
1.点击右上方的有线连接: 2.点击编辑连接: 3.选择802.x的安全性分页: 4.勾选下面的单选框: 5.输入登陆账号(通常就是学号),密码(通常是身份证后六位),如果出现了一个“解锁密码环”的对 ...
- 软件安装失败,导致ubuntu软件中心软件消失
感谢百度上各位IT界朋友的帮助,由于某个软件安装失败,导致ubuntu软件中心软件消失的解决办法: 找百度,有人说, 使用命令:sudo apt-get install software-center ...
- Linux启动流程CentOS6
1.运行级别 0 关机 1 单用户模式,可以想象为Windows的安全模式,主要用与系统修复 2 不完全的命令行模式,不含NFS服务 3 完全的命令行模式,就是标准字符界面 4 系统保留 5 图像模式 ...
- java常用类 --- Object
Object类 Object类是所有Java类的父类,其位于java.lang包中.任何Java对象,如果没有显示定义父类则它默认Object类作为父类. 方法如下: 其中与线程相关的有5个方法: n ...
- VTK初学一,e_Triangle三角形的绘制
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- 电脑开机黑屏,显示Reboot and Select proper boot device!
“reboot and select proper boot device or insert boot media in selected boot device and press a key” ...
- Ali相关面试题
接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...
- hihoCoder挑战赛11.题目4 : 高等理论计算机科学(LCA)
clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以... 题目链接:http://hihocoder.com/problemset/problem/1167 ...
- C++之map、list操作
#include <iostream> #include "map_struct.h" #include <map> using namespace std ...
- php综合应用
php面试题之五--PHP综合应用(高级部分) 五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transf ...