在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

示例:

输入:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 输出: 4
class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
return 0; int max = 0;
int[] heights = new int[matrix[0].length];
for (char[] line : matrix) {
for (int i = 0; i < line.length; i++)
heights[i] = line[i] == '0' ? 0 : heights[i] + 1; int temp = findMaxSize(heights);
max = max > temp ? max : temp;
} return max;
} private int findMaxSize(int[] heights) {
Deque<Integer> stack = new LinkedList<>();
int maxSize = 0; for (int i = 0; i < heights.length; i++) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
int t = stack.pop();
int left = stack.isEmpty() ? - 1 : stack.peek();
int right = i;
int minLength = heights[t] < right - left - 1 ? heights[t] : right - left - 1;
maxSize = maxSize > minLength * minLength ? maxSize : minLength * minLength;
} stack.push(i);
} while (!stack.isEmpty()) {
int t = stack.pop();
int left = stack.isEmpty() ? t - 1 : stack.peek();
int right = heights.length - 1;
int minLength = heights[t] < right - left ? heights[t] : right - left;
maxSize = maxSize > minLength * minLength ? maxSize : minLength * minLength;
} return maxSize;
}
}

Q221 最大正方形的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  3. 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。

    查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  4. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  5. 洛谷 P1387 最大正方形 Label:奇怪的解法

    题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...

  6. H5一行显示两个正方形

    1)有时候一些图片会是正方形或者长方形,对于这样的图片一般都是居中显示到正方体内,代码如下:  .exhibition_list img{width:100%;position: relative;t ...

  7. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  8. for 循环 正方形

    <?php//================================正方形//for($q = 1; $q <= 5; $q ++ ){//    for($z =1; $z & ...

  9. CodeForces 459A Pashmak and Garden(水~几何-给两点求两点组成正方形)

    题目链接:http://codeforces.com/problemset/problem/459/A 题目大意: 给出两个点(在坐标轴中),求另外两个点从而构成一个正方形,该正方形与坐标轴平行. 如 ...

随机推荐

  1. 安装系统重启的时候出现了error:file '/boot/grub/i386-pc/normal.mod' not found

    1.直接进入系统的时候只出现grub rescue的命令行 可以使用的命令有set和 ls 在用ls命令查看 磁盘的分区情况其中hd0 代表第一块硬盘 hd1代表第二块 使用ls 来查看存在那些系统, ...

  2. open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory 解决方案

    方法一.  yum安装 yum install *rhsm* 方法二 (我是用这方法解决的) 执行命令: ①   wget http://mirror.centos.org/centos/7/os/x ...

  3. [转]如何创建一个自签名的SSL证书(X509)

    原文出自:http://www.joyios.com/?p=47 引言 使用HTTP(超文本传输)协议访问互联网上的数据是没有经过加密的.也就是说,任何人都可以通过适当的工具拦截或者监听到在网络上传输 ...

  4. mvc数组绑定-jquery ajax

    var list=[];//数组 list[0]=1001; list[1]=1002; list[1]=1003; var json_data = { selected: list}; $.ajax ...

  5. 试题 H: 人物相关性分析 第十届蓝桥杯

    试题 H: 人物相关性分析时间限制: 1.0s 内存限制: 512.0MB 本题总分: 20 分[问题描述]小明正在分析一本小说中的人物相关性.他想知道在小说中 Alice 和 Bob有多少次同时出现 ...

  6. jsp中路径的写法

    在JavaWeb开发中,常使用绝对路径的方式来引入JavaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况 代码” ${pageContext.request.context ...

  7. [翻译] Writing Property Editors 编写属性编辑器

    Writing Property Editors 编写属性编辑器   When you select a component in the designer its properties are di ...

  8. [Elixir005] 查看指定数据的详细信息 i helper

    elixir在1.2后增加了一个新的特性i helper. 在iex shell中使用i可以查看任意数据的数据类型和详细描述 #查看变量描述 iex(1)> i {:test, "Th ...

  9. Buffer Pool--内存总结1

    物理地址空间是处理器用来访问位于总线上的所有部件的集合.在32位处理器上,地址总线为32位,寻址空间为4GB.在使用PAE的32位服务器上,地址总线为36位,寻址空间为64GB.在64位的处理器上生产 ...

  10. 配置ssh使用socks代理

    ssh -o ProxyCommand='nc -x 127.0.0.1:1080 %h %p' username@server