【Leetcode】Maximal Square

题目链接:https://leetcode.com/problems/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.

思路:

用c[i][j]表示在长为i、宽为j的矩阵里面,包含matrix[i][j]这个点的最大正方形的长。

显然c[i][j]的大小跟c[i-1][j]、c[i][j-1]、c[i-1][j-1]有关。画图观察c[i-1][j]、c[i][j-1]、c[i-1][j-1]包围起来的面积

可知 c[i][j] = min(c[i-1][j]、c[i][j-1]、c[i-1][j-1]) +1

算法

public int maximalSquare(char[][] matrix) {
        if (matrix.length == 0)
            return 0;
        int max = 0;
        int c[][] = new int[matrix.length][matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == '0') {
                    continue;
                }
                if (i == 0 || j == 0) {// 在边缘时
                    c[i][j] = 1;
                } else {
                    c[i][j] = Math.min(c[i - 1][j - 1], Math.min(c[i - 1][j], c[i][j - 1])) + 1;
                }
                max = Math.max(max, c[i][j]);
            }
        }
        return max * max;
    }  
时间: 2024-11-08 22:31:28

【Leetcode】Maximal Square的相关文章

【leetcode】Maximal Rectangle

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

【leetcode】Maximal Rectangle (hard)★

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 找到01矩形中最大的全1子矩阵. 我自己的思路: 我先用一个跟输入相同大小的矩阵numInCol 存储从当前位置开始向下有多少连续的1. 如 1 0 1 0 1 1 1 1 1 其numInCol 是 1 0 3 0 2 2 1 1 1 然后用一个变量tmpans

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n