leetcode 221

题意:给定一个0,1矩阵,找到最大的包含1的正方形,并返回它的面积。

思路:动态规划

初始化:二维数组:dp[i][j] 表示 到达(i, j )位置所能组成的最大正方形的边长。

1)边界条件:i表示行数,j表示列数。 i == 0 || j == 0

2)状态转移方程:matrix[i][j] == 1时,dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j] ) ) +1

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if(matrix.empty() || matrix[0].empty()) return 0;

        int n = matrix.size();
        int m = matrix[0].size();

        int dp[n][m] = {};  //最长边长
        int res = 0; //面积
        for(int i=0; i<n; i++){
            for(int j =0;j<m; j++){
                if(i==0 || j==0)
                    dp[i][j] = matrix[i][j] - ‘0‘;
                else if(matrix[i][j] == ‘1‘){
                    dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) +1;
                }
                res = max(dp[i][j], res);
            }
        }
        return res*res;
    }
};

原文地址:https://www.cnblogs.com/Bella2017/p/10884963.html

时间: 2024-10-05 06:55:38

leetcode 221的相关文章

[leetcode] 221. 最大正方形

221. 最大正方形 其实这题是85. 最大矩形的特殊情况,我们将85题代码稍微改一下,然后直接套用即可. 此题要求是正方形,那么我们在计算长与宽时,取短的那条然后平方即可. class Solution { public int maximalSquare(char[][] matrix) { return maximalRectangle(matrix); } public int maximalRectangle(char[][] matrix) { int m = matrix.leng

(DP 线性DP) leetcode 221. Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 =============================================================== 这是一个D

[LeetCode] 221. 最大正方形(DP)

题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximal-square 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 题解 dp[i][j] 表示以matrix[i-1][j-1]为右下角的最大正方形的边长 转移方

【LeetCode 221】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. 思路: DP,到达当前(非0)结点的最长边长square[i][j] = min(squa

Java for LeetCode 221 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. 解题思路: dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候

(medium)LeetCode 221.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. Credits:Special thanks to @Freezen for adding

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

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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. Credits:Special thanks to @Freezen for addin

Leetcode 221.最大的正方形

最大的正方形 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了.这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "ho