在一个由 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. shell 用环境变量的值修改properties文件

    假设有如下属性文件 demo.properties user.name=test user.password=123456 ............................... 需求:先需要 ...

  2. 转 group_concat函数详解

    MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...

  3. 移动lob类型索引到指定表空间

    WWWNEWSAD 为表名 USERS 为原表空间 CONTENT 为lob类型的字段 DATA_INDEX_WXZJ 指定的表空间 ALTER TABLE WWWNEWSAD MOVE TABLES ...

  4. (转)C# .net微信开发,开发认证,关注触发消息,自动应答,事件响应,自定义菜单

    原文地址:http://www.cnblogs.com/qidian10/p/3492751.html 成为开发者 string[] ArrTmp = { "token", Req ...

  5. [转]解决Mysql InnoDB: Failing assertion: ret || !assert_on_error问题

    国庆回来后,发现mysql停止服务了,没办法继续启动了.查看日志,看到: 131008 09:56:03 mysqld_safe Starting mysqld daemon with databas ...

  6. Eclipse中Tomcat Server启动后马上又自动停止报错Address已经使用8005端口 Can't assign requested address (Bind failed)

    Eclipse中Tomcat Server启动后马上又自动停止报错 Can't assign requested address (Bind failed) ,打开Tomcat Server的配置页面 ...

  7. java.lang.IllegalArgumentException: No converter found for return value of type: class com.smart.result.Page

    今天学习了一下spring boot 中的mybatis,用mybatis来增删改查用户,获取用户,添加用户,修改用户,删除用户,修改用户,都是可以的,但是获取带分页的用户列表,一直抛出这个java. ...

  8. 【C#】在datatable中添加一序号列,编号从1依次递增,并且在第一列

    详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak/// <summary> ...

  9. 开源NFV管理器 - OpenStack Tacker介绍 NFV和Tacker介绍和主要功能

    原文链接:https://blog.csdn.net/bc_vnetwork/article/details/51463518 1.NFV概述 NFV(网络功能虚拟化Network Function ...

  10. OpenStack 业务链networking-sfc介绍 (1) - 概述

    原文链接:https://blog.csdn.net/bc_vnetwork/article/details/65630355 1.  Service Function Chain概述 Neutron ...