LeetCode每日一题,2025-08-21

全0子数组

重点是每次加上i-last,i是最右边的0的位置,左边可选的位置为[last+1,last+2,...i]i-last

java 复制代码
class Solution {
    public long zeroFilledSubarray(int[] nums) {
        long ans = 0;
        int last = -1;
        for (int i = 0; i < nums.length; i++) {
            int x = nums[i];
            if (x != 0) {
                last = i; // 记录上一个非 0 元素的位置
            } else {
                ans += i - last;
            }
        }
        return ans;
    }
}

统计全为1的正方形矩阵

这题重点是枚举上下两个边界,确定了边界,就确定了边长。然后题目变成了,统计有多少长度为h的全h子数组

java 复制代码
class Solution {
    public int countSquares(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int ans = 0;
        for (int top = 0; top < m; top++) { // 枚举上边界
            int[] a = new int[n];
            for (int bottom = top; bottom < m; bottom++) { // 枚举下边界
                int h = bottom - top + 1; // 高
                // 2348. 全 h 子数组的数目
                int last = -1;
                for (int j = 0; j < n; j++) {
                    a[j] += matrix[bottom][j]; // 把 bottom 这一行的值加到 a 中
                    if (a[j] != h) {
                        last = j; // 记录上一个非 h 元素的位置
                    } else if (j - last >= h) { // 右端点为 j 的长为 h 的子数组全是 h
                        ans++;
                    }
                }
            }
        }
        return ans;
    }
}

提取出来, T 2348. 全 h 子数组的数目

java 复制代码
int last = -1;
for (int j = 0; j < n; j++) {
    a[j] += matrix[bottom][j]; // 把 bottom 这一行的值加到 a 中
    if (a[j] != h) {
        last = j; // 记录上一个非 h 元素的位置
    } else if (j - last >= h) { // 右端点为 j 的长为 h 的子数组全是 h
        ans++;
    }
}

统计全1子矩形

这个比上一题简单,确定了高度h以后,再加一个第一题就可以了

java 复制代码
class Solution {
    public int numSubmat(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        int ans = 0;
        for (int top = 0; top < m; top++) { // 枚举上边界
            int[] a = new int[n];
            for (int bottom = top; bottom < m; bottom++) { // 枚举下边界
                int h = bottom - top + 1; // 高
                // 2348. 全 h 子数组的数目
                int last = -1;
                for (int j = 0; j < n; j++) {
                    a[j] += mat[bottom][j]; // 把 bottom 这一行的值加到 a 中
                    if (a[j] != h) {
                        last = j; // 记录上一个非 h 元素的位置
                    } else {
                        ans += j - last;
                    }
                }
            }
        }
        return ans;
    }
}
相关推荐
SamsongSSS25 分钟前
JavaScript逆向SM国密算法
javascript·算法·逆向
图灵信徒27 分钟前
2025 ICPC Gran Premio de Mexico 3ra Fecha
c++·算法·codeforcrs
大锦终29 分钟前
【算法】栈专题
数据结构·c++·算法·leetcode
haogexiaole29 分钟前
资源图分配算法
算法
天选之女wow32 分钟前
【代码随想录算法训练营——Day6(Day5周日休息)】哈希表——242.有效的字母异位词、349.两个数组的交集、202.快乐数、1.两数之和
数据结构·算法·leetcode·散列表
寒冬没有雪34 分钟前
利用归并算法对链表进行排序
c++·算法
CoovallyAIHub34 分钟前
AI帮你打标签!这个开源神器让数据标注快了90%
深度学习·算法·计算机视觉
古译汉书34 分钟前
蓝桥杯算法之基础知识(7)---排序题的快排和归并排序
算法
薛定谔的算法40 分钟前
JavaScript队列实现详解:从基础到性能优化
javascript·数据结构·算法
pan0c2341 分钟前
机器学习 之 时间序列预测 的 电力负荷预测案例
人工智能·算法·机器学习