当前位置: 首页 > news >正文

LeetCode每日一题,2025-08-21

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

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子数组

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 的子数组全是 hans++;}}}}return ans;}
}

提取出来, T 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 的子数组全是 hans++;}
}

统计全1子矩形
这个比上一题简单,确定了高度h以后,再加一个第一题就可以了

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;}
}
http://www.dtcms.com/a/341945.html

相关文章:

  • C++——C++重点知识点复习2(详细复习模板,继承)
  • 2.Shell脚本修炼手册---创建第一个 Shell 脚本
  • C++ string类(reserve , resize , insert , erase)
  • 鸿蒙中网络诊断:Network分析
  • 深入理解JVM内存结构:从字节码执行到垃圾回收的全景解析
  • 金山云Q2营收23.5亿元 AI战略激活业务增长新空间
  • Altium Designer 22使用笔记(8)---PCB电气约束设置
  • GitHub Copilot - GitHub 推出的AI编程助手
  • Pytorch框架学习
  • Bigemap APP 详细使用教程,入门学习PPT
  • element table 表格多选框选中高亮
  • KubeBlocks for ClickHouse 容器化之路
  • 【运维进阶】shell三剑客
  • DeepSeek大模型如何重塑AI Agent?从技术突破到行业落地
  • 环境搭建-dockerfile构建镜像时apt软件包出现exit100错误+ pip下载python库时下载过慢的解决方法
  • SpringWeb详解
  • CorrectNav——基于VLM构建带“自我纠正飞轮”的VLN:通过「视觉输入和语言指令」预测导航动作,且从动作和感知层面生成自我修正数据
  • 【LeetCode热题100道笔记+动画】三数之和
  • Linux上安装MySQL 二进制包
  • TENON AI-AI大模型模拟面试官
  • idea进阶技能掌握, 自带HTTP测试工具HTTP client使用方法详解,完全可替代PostMan
  • 【力扣 买卖股票的最佳时机 Java/Python】
  • 数据库架构开发知识库体系
  • VGG改进(3):基于Cross Attention的VGG16增强方案
  • Foundry与Uniswap V2实战开发指南
  • 【自记】Power BI 中 DISTINCT 和 ALLNOBLANKROW 的区别说明
  • 比特分割 + 尖峰保留:FlashCommunication V2 实现任意比特通信与 3.2× 加速
  • 一键授权登录
  • Windows暂停更新10年最简单的设置
  • UNet改进(33):基于CBAM原理与PyTorch实战指南