1.问题描述

给定一个由'0'和'1'组成的2D空间,找到这个空间中只包含1的最大矩阵

2.解题思路

将此问题转换为求只包含'1'的左边界和右边界问题

3.程序源码

public int maximalRectangle(char[][] matrix) {
            if(matrix==null || matrix.length==0 || matrix[0].length==0)
                return 0;
            int max = 0;
            int rows = matrix.length; //行数
            int cols = matrix[0].length;//列数
            int[] height = new int[cols];//第i行第j列直方图的高
            int[] left = new int[cols];  //左边界(与高度相关进行判断)
            int[] right = new int[cols]; //右边界
            for(int i=0; i<cols; i++){
                right[i] = cols;
            }
            for(int i=0; i<rows; i++){//第i层
                int curLeft = 0;
                int curRight = cols;   //当前左边界和右边界
                //更新左边界
                for(int j=0; j<cols; j++){
                    if(matrix[i][j] == '1'){
                        height[j]++;
                        left[j] = Math.max(curLeft, left[j]);
                    }else{
                        height[j] = 0;
                        curLeft = j+1;
                        left[j] = 0;
                    }
                }
                for(int j=cols-1; j>=0; j--){ //更新右边界
                    if(matrix[i][j]=='1'){
                        right[j] = Math.min(curRight, right[j]);  //很重要的一步
                    }else{
                        right[j] = cols;
                        curRight = j;
                    }
                    //max = Math.max(max, (right[j]-left[j])*height[j]);
                }
                for(int k=0;k<cols;k++){
                    if(max<(right[k]-left[k])*height[k]){
                        max=(right[k]-left[k])*height[k];
                    }
                }
            }
            return max;
        }

MaximalRectangle相关推荐

  1. LeetCode——maximal-rectangle

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

  2. maximal-rectangle

    题目:牛客网 解题思路: 利用递增栈,可参考https://blog.csdn.net/qq_17550379/article/details/85093224 这种解法显然很慢,我们有一种更好的思路 ...

  3. java基础编程题(2)

    1.给定一个二叉树,找出其最大深度. 注:二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. /*** Definition for a binary tree node.* public cl ...

  4. 语音计算矩形面积_LeetCode85-最大矩形

    今天在制作书签的时候 突然想到了一个问题 如果要送给未来的女朋友一个书签 上面该写些什么话 哈哈哈哈哈哈哈哈哈 The Spring is coming! 想了一会儿,觉得这句话最合适 To xxx: ...

  5. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  6. LeetCode 85. Maximal Rectangle --python,java解法

    题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  7. Leetcode: Maximal Rectangle

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

  8. Leetcode | Maximal Rectangle

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

  9. 85. Maximal Rectangel

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

最新文章

  1. 上海交大与《Science》发布125个最具挑战的科学问题
  2. sscanf()的用法和例子
  3. MySQL_控制台操作_01
  4. 线程Java的两种方式_多线程(java和guava两种方式):
  5. mfs分布式存储+master端高可用
  6. PyCharm设置自动换行
  7. 转换背景色 html,html – CSS转换:淡化背景颜色,重置后
  8. 细分shared_ptr智能指针在各个版本的使用情况
  9. Introduction to Latent Dirichlet Allocation
  10. HTML5+CSS期末大作业:个人网站设计——响应式个人简历介绍网页(5页) HTML+CSS+JavaScript
  11. 自由软件运动与GNU项目
  12. 联想昭阳E46A不能上网
  13. 无线AP与无线路由的区别
  14. 漏洞修复:Often Misused: HTTP Method Override
  15. 计算机考研怎么给老师发邮件,2015考研复试给导师的邮件怎么写?_跨考网
  16. 单片机编程软件很简单(17),Keil单片机编程软件之编译、链接
  17. HTTP协议中的一些概念
  18. F2FS文件系统架构与原理分析(五)——元数据组织及管理
  19. Windows10任务计划怎么用
  20. 字符串转换成整数-----字符串‘123456’转换成数值123456(指针)

热门文章

  1. 我的世界java版如何加入hypixel_我的世界hypixel服务器怎么组队 hypixel服务器组队方法介绍...
  2. CF1463-A. Dungeon
  3. IOS开发大神装逼神器
  4. MySQL 替换手机号码中间4位为星号显示
  5. 微信支付要租用服务器吗,切记!使用微信支付一定要打开这些功能!
  6. iMX6UL配置MCP2515模块(SPI转CAN)——基于迅为iTOP-iMX6UL开发板
  7. 2013上半年 系统集成项目管理工程师 下午试卷
  8. 华为鸿蒙系统8月开源,华为正式宣布,鸿蒙系统8月开源,任正非却说:优先选用安卓...
  9. 题目 1567: 超级玛丽
  10. KanziStudio应用程序配置详解