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;
    }
}
相关推荐
得物技术12 分钟前
得物推荐系统诊断 Agent:从 “调接口” 到 “会思考”|AICon 演讲整理
人工智能·算法·架构
Lugas20 分钟前
为啥说男生找对象尽量在25岁前找到?
算法
MrZhao40023 分钟前
从能跑到可用:一个 Agent Harness 还差哪些工程闭环?
算法
QN1幻化引擎1 小时前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法
学计算机的计算基1 小时前
LeetCode 图论四题精讲:BFS、拓扑排序、Trie 树的模板与优化
java·笔记·算法
浩瀚地学1 小时前
【面试算法笔记】0202-链表-基本功能实现
java·经验分享·笔记·算法·面试
tkevinjd2 小时前
416分割等和子集
java·python·算法·leetcode·职场和发展
Keven_112 小时前
算法札记:Tarjan与拓扑序(Topo)的关系
算法·拓扑·tarjan
ShallWeL2 小时前
AUC、F1、召回率怎么选
人工智能·算法·机器学习
水龙吟啸2 小时前
华为2026.6.3机考选择题+编程题【速刷敲黑板】
人工智能·深度学习·算法·华为