C++ | Leetcode C++题解之第221题最大正方形

题目:

题解:

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return 0;
        }
        int maxSide = 0;
        int rows = matrix.size(), columns = matrix[0].size();
        vector<vector<int>> dp(rows, vector<int>(columns));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (matrix[i][j] == '1') {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                    }
                    maxSide = max(maxSide, dp[i][j]);
                }
            }
        }
        int maxSquare = maxSide * maxSide;
        return maxSquare;
    }
};

相关推荐

  1. 221. 正方形

    2024-07-09 18:10:06       43 阅读
  2. LeetCode 221. 正方形

    2024-07-09 18:10:06       5 阅读
  3. 力扣221. 正方形

    2024-07-09 18:10:06       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 18:10:06       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 18:10:06       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 18:10:06       4 阅读
  4. Python语言-面向对象

    2024-07-09 18:10:06       4 阅读

热门阅读

  1. mongodb-docker-compos-安装

    2024-07-09 18:10:06       14 阅读
  2. aws slb

    2024-07-09 18:10:06       8 阅读
  3. django学习入门系列之第四点《案例 登录》

    2024-07-09 18:10:06       15 阅读
  4. Rust破界:前端革新与Vite重构的深度透视(中)

    2024-07-09 18:10:06       10 阅读
  5. uniapp中微信小程序——蓝牙连接并通信

    2024-07-09 18:10:06       13 阅读
  6. 被裁了(9年)

    2024-07-09 18:10:06       10 阅读