Leetcode 1277. 统计全为 1 的正方形子矩阵 动态规划

原题链接:Leetcode 1277. 统计全为 1 的正方形子矩阵

cpp 复制代码
class Solution {
public:
    int countSquares(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = 0;
                if (matrix[i][j] == 1) {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]),
                                       dp[i - 1][j - 1]) +
                                   1;
                    }
                }
                res += dp[i][j];
            }
        }
        return res;
    }
};
相关推荐
Navigator_Z5 小时前
LeetCode //C - 1240. Tiling a Rectangle with the Fewest Squares
c语言·算法·leetcode
稻米哟5 小时前
力扣100——双指针
算法·leetcode
Raas1008 小时前
MAI Gateway(魔芋企业级AI网关)功能全解:AI网关有哪些功能?一文看懂AI网关能力矩阵
大数据·人工智能·矩阵·gateway·mai gateway·企业级产品
橘子汽水1689 小时前
Leetcode 198,118打家劫舍,杨辉三角
算法·leetcode
alphaTao13 小时前
LeetCode 每日一题 2026/9/7-2026/9/13
算法·leetcode
土司大王16 小时前
LeetCode 79 单词搜索:Java 回溯模板、网格 DFS 与剪枝优化
java·算法·leetcode·深度优先
All for pursuit.16 小时前
【数组-5】560.和为K的子数组
数据结构·c++·算法·leetcode
土司大王2 天前
LeetCode 17 电话号码的字母组合:Java 回溯模板、多叉决策树与复杂度分析
java·leetcode·决策树