085 Maximal Rectangle 最大矩形
生活随笔
收集整理的這篇文章主要介紹了
085 Maximal Rectangle 最大矩形
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個填充了 0 和 1 的二進制矩陣,找到最大的只包含 1 的矩形并返回其面積。
例如,給出以下矩陣:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 6
詳見:https://leetcode.com/problems/maximal-rectangle/description/
Java實現:
class Solution {public int maximalRectangle(char[][] matrix) {if(matrix == null || matrix.length == 0 || matrix[0].length == 0){return 0;}int res = 0;int m = matrix.length;int n = matrix[0].length;int [] heights = new int[n];for(int i = 0; i<m; i++){ for(int j = 0; j<n; j++){heights[j] = matrix[i][j] == '0' ? 0 : heights[j]+1; }res = Math.max(res, largestRectangleArea(heights));}return res;}private int largestRectangleArea(int[] heights) {int res=0;int n=heights.length;for(int i=0;i<n;++i){if(i+1<n&&heights[i]<=heights[i+1]){continue;}int minH=heights[i];for(int j=i;j>=0;--j){minH=Math.min(minH,heights[j]);int area=minH*(i-j+1);res=Math.max(res,area);}}return res;} }?參考:https://www.cnblogs.com/grandyang/p/4322667.html
轉載于:https://www.cnblogs.com/xidian2014/p/8715614.html
總結
以上是生活随笔為你收集整理的085 Maximal Rectangle 最大矩形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到抓小鱼小虾是什么意思
- 下一篇: 梦到缺钱是怎么回事