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;
    }
};
相关推荐
YuTaoShao1 天前
【LeetCode 每日一题】1277. 统计全为 1 的正方形子矩阵
算法·leetcode·矩阵
野犬寒鸦1 天前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
阿昭L1 天前
leetcode两数之和
算法·leetcode
Lris-KK1 天前
【Leetcode】高频SQL基础题--1164.指定日期的产品价格
sql·leetcode
Swift社区1 天前
Swift 解法详解:LeetCode 371《两整数之和》
开发语言·leetcode·swift
Swift社区1 天前
Swift 解法详解 LeetCode 362:敲击计数器,让数据统计更高效
开发语言·leetcode·swift
一只鱼^_2 天前
牛客周赛 Round 108
数据结构·c++·算法·动态规划·图论·广度优先·推荐算法
小刘的AI小站2 天前
leetcode hot100 二叉搜索树
算法·leetcode
自信的小螺丝钉2 天前
Leetcode 876. 链表的中间结点 快慢指针
算法·leetcode·链表·指针
红豆怪怪2 天前
[LeetCode 热题 100] 32. 最长有效括号
数据结构·python·算法·leetcode·动态规划·代理模式