题目中让找出有多少个子矩阵,其元素的和为target.
子矩阵的范围:x1 <= x <= x2, y1 <= y <= y2
4个边界有任一个不同,都算不同的子矩阵。

思路

有一种简单粗暴的方法,就是遍历所有的x1, x2, y1, y2,
用积分矩阵求和。

public int numSubmatrixSumTarget(int[][] matrix, int target) {int m = matrix.length;int n = matrix[0].length;// aux[i][j] is the sum of sub-matrix which start at [0][0] and end in [i][j]int[][] aux = new int[m + 1][n + 1]; // padding on top and leftfor (int i = 1; i < m + 1; i++) {for (int j = 1; j < n + 1; j++) {aux[i][j] = matrix[i - 1][j - 1] + aux[i - 1][j] + aux[i][j - 1] - aux[i - 1][j - 1]; }}int res = 0;// try each sub-matrixfor (int x1 = 1; x1 < m + 1; x1++) {for (int y1 = 1; y1 < n + 1; y1++) {for (int x2 = x1; x2 < m + 1; x2++) {for (int y2 = y1; y2 < n + 1; y2++) {if (target == aux[x2][y2] - aux[x2][y1 - 1] - aux[x1 - 1][y2] + aux[x1 - 1][y1 - 1])res++;}}}}return res;
}

上面的方法能通过,但是比较慢。时间上排名比较靠后。
找到了下面这种方法,

可以想象一下行的上边界是top, 下边界是bottom
然后在top~bottom这些行的范围内,一条一条(一列一列)地求和,
和保存在sum数组里,sum[col] 表示 col这一列top~bottom范围的和。

这样子矩阵的上下边界确定好了,再确定左右边界,
只需要求left ~ right范围内sum[col]的和就行了,每个sum[col]是一条,多条组合起来就是一个子矩阵。

一旦和满足==target,就算找到一个子矩阵。
遍历top : 1~m-1, bottom : top~m-1, left : 0 ~n-1, right: left ~ n-1

public int numSubmatrixSumTarget(int[][] matrix, int target) {int m = matrix.length;int n = matrix[0].length;int res = 0;// traverse upper boundaryfor (int top = 0; top < m; top++) {// for each upper boundary, we have a prefix sum arrayint[] sum = new int[n];// traverse lower boundaryfor (int bottom = top; bottom < m; bottom++) {// count the prefix sum for each columnfor (int col = 0; col < n; col++) {sum[col] += matrix[bottom][col];}// traverse left and right boundaryfor (int left = 0; left < n; left++) {int cnt = 0;for (int right = left; right < n; right++) {cnt += sum[right];if (cnt == target) res++;}}}}return res;
}

leetcode 1074. Number of Submatrices That Sum to Target(和为target的子矩阵个数)相关推荐

  1. LeetCode Singer Number

    Python LeetCode Singer Number 题目说明 算法1 数学方法 算法2 异或运算 题目说明 给定一个列表,其中包含数对相同元素与一个单独元素,例如[2,2,1].[1,3,5, ...

  2. leetcode Sum 系列----寻找和为定值的多个数

    july 大神有个程序员编程艺术系列,第五章<寻找和为定值的多个数>,现在我们站在大牛的肩膀上,对leetcode上n个数求和的系列问题做个阶段性总结. 1.leetcode No.1 2 ...

  3. 【LeetCode】#39组合总和(Combination Sum)

    [LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...

  4. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  5. LeetCode 200. Number of Islands--c++ dfs解法

    LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Pyt ...

  6. 【LeetCode从零单排】No129 Sum Root to Leaf Numbers

    题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...

  7. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

  8. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

  9. LeetCode小白菜笔记[1]:Two Sum

    LeetCode小白菜笔记[1]:Two Sum 1. Two Sum [Easy] 题目: Given an array of integers, return indices of the two ...

最新文章

  1. MapReduce编程初体验
  2. PHP session有效期session.gc_maxlifetime
  3. Pandas 文本数据方法 is*()
  4. Facebook发布张量理解库,自动编译高性能机器学习核心
  5. C#的TextBox控件输入测试-只允许输入数字的测试:
  6. curl的安装与配置
  7. 计算机应用专业招聘试讲内容,广东文艺职业学院2018年第二批合同制人员招聘专业技能考核和试讲题目...
  8. mooc-人工智能与信息社会-人工智能发展简史
  9. 闭环系统零、极点位置对时间响应性能指标的影响
  10. win8-cmd请求的操作需要提升
  11. 随机梯度下降法(stochastic gradient descent,SGD)
  12. 彻底解决MySQL导入Excel无法打开Excel的问题
  13. CVE-2021-40539-Zoho ManageEngine ADSelfService Plus如何从bypass到RCE
  14. 互联网用户公众账号信息服务管理规定
  15. 英雄联盟 LCUAPI
  16. 李彦宏现身巴黎Viva科技大会:机遇常在,未来通过科技和创新改变生活
  17. 【34个PMP项目实战案例5】如何提高团队的配合度
  18. OSChina 周三乱弹 ——你未来老婆的背影
  19. 【NEON Intrincics加速计算案例】使用neon加速计算视频FullRange转LimitedRange
  20. iterm2连接远程服务器

热门文章

  1. 嵌入式 STM32 串口波特率生成器BRR的值计算笔记
  2. 对论文写作专栏文章的简单归纳总结和心得体会
  3. Redis启动、停止、查看
  4. 能被2,3,4,5,6,7,8,9...等数整除的数特征
  5. 牛客寒假算法基础集训营5 J 炫酷数学
  6. unsigned long long 与 long long
  7. Java开源工具库使用之java源代码生成库JavaPoet
  8. 献给android原生应用层开发初学者技术架构选型和整合的方案思路(二)
  9. 动态合批和静态合批的区别
  10. EXCEL中对一列数据每隔n行提取数据出来