leetcode 304. 二维区域和检索 - 矩阵不可变(前缀和)

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) 。

上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。

示例:

给定 matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

解题思路

维护一个前缀和数组,公式: temp[i][j]=matrix[i][j]+temp[i-1][j]+temp[i][j-1]-temp[i-1][j-1];
根据前缀和计算区域和公式:sum=temp[row2][col2]-temp[row2][col1-1]-temp[row1-1][col2]+temp[row1][col1]

代码

   class NumMatrix {int[][] temp;public NumMatrix(int[][] matrix) {if(matrix.length==0) return;temp=new int[matrix.length][matrix[0].length];temp[0][0]=matrix[0][0];  //计算前缀和数组for (int i = 1; i < matrix.length; i++) {temp[i][0]=temp[i-1][0]+matrix[i][0];}for (int i = 1; i < matrix[0].length; i++) {temp[0][i]=temp[0][i-1]+matrix[0][i];}for (int i = 1; i < matrix.length; i++)for (int j = 1; j < matrix[0].length; j++){
//前缀和公式temp[i][j]=matrix[i][j]+temp[i-1][j]+temp[i][j-1]-temp[i-1][j-1];}}public int sumRegion(int row1, int col1, int row2, int col2) {//区域矩阵和=temp[row2][col2]-temp[row2][col1-1]-temp[row1-1][col2]+temp[row1][col1]int leftUpRow=row1-1,leftUpcol=col1-1,sum=temp[row2][col2];if(leftUpcol>=0&&leftUpRow>=0)sum+=temp[leftUpRow][leftUpcol];if(leftUpcol>=0) sum-=temp[row2][leftUpcol];if(leftUpRow>=0) sum-=temp[leftUpRow][col2];return sum;}}/*** Your NumMatrix object will be instantiated and called as such:* NumMatrix obj = new NumMatrix(matrix);* int param_1 = obj.sumRegion(row1,col1,row2,col2);*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391681.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法训练营 重编码_编码训练营后如何找到工作

算法训练营 重编码by Roxy Ayaz由Roxy Ayaz 编码训练营后如何找到工作 (How to get a job after a coding bootcamp) Getting a tech job after a coding bootcamp is very possible, but not necessarily pain-free.在编码训练营之后获得技术工作是很有可能的&#xff0c;但不…

dt决策树_决策树:构建DT的分步方法

dt决策树介绍 (Introduction) Decision Trees (DTs) are a non-parametric supervised learning method used for classification and regression. The goal is to create a model that predicts the value of a target variable by learning simple decision rules inferred f…

读C#开发实战1200例子记录-2017年8月14日10:03:55

C# 语言基础应用&#xff0c;注释 "///"标记不仅仅可以为代码段添加说明&#xff0c;它还有一项更重要的工作&#xff0c;就是用于生成自动文档。自动文档一般用于描述项目&#xff0c;是项目更加清晰直观。在VisualStudio2015中可以通过设置项目属性来生成自动文档。…

iOS端(腾讯Bugly)闪退异常上报扑获日志集成与使用指南

app已经上架并且有三次更新版本&#xff0c;今天市场部和顾客约谈时&#xff0c;发现顾客的iphone 6 plus iOS 9.0.2上运行app点击登录按钮时直接闪退&#xff0c;无法进入app里&#xff0c;这个问题还是第一次遇到&#xff0c;我下载了相应的模拟器版本&#xff0c; 并在上面运…

数据特征分析-正太分布

期望值&#xff0c;即在一个离散性随机变量试验中每次可能结果的概率乘以其结果的总和。 若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布&#xff0c;记为N(μ&#xff0c;σ^2)&#xff0c;其概率密度函数为正态分布的期望值μ决定了其位置&#xff0c;其标准差σ决定…

leetcode 338. 比特位计数

给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i &#xff0c;计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 解题思路 偶数&#xff1a;和偶数除以2以后的数字&#xff0c;具有相…

r语言调用数据集中的数据集_自然语言数据集中未解决的问题

r语言调用数据集中的数据集Garbage in, garbage out. You don’t have to be an ML expert to have heard this phrase. Models uncover patterns in the data, so when the data is broken, they develop broken behavior. This is why researchers allocate significant reso…

为什么不应该使用(长期存在的)功能分支

Isn’t the git history in the picture above nice to work with? There are probably many problems in that repository, but one of them is most definitely the use of feature branches. Let’s see how such a bad thing became such a common practice.上面图片中的g…

(转) Spring 3 报org.aopalliance.intercept.MethodInterceptor问题解决方法

http://blog.csdn.net/henuhaigang/article/details/13678023 转自CSDN博客&#xff0c;因为一个jar包没引入困扰我好长时间 &#xff0c;当时正在做spring AOP 的一个小测试&#xff0c;总在报错&#xff0c;最后发现自己是问题3&#xff0c;引入一个jar包得到了解决 一 开发环…

数据特征分析-相关性分析

相关性分析是指对两个或多个具备相关性的变量元素进行分析&#xff0c;从而衡量两个变量的相关密切程度。 相关性的元素之间需要存在一定的联系或者概率才可以进行相关性分析。 相关系数在[-1,1]之间。 一、图示初判 通过pandas做散点矩阵图进行初步判断 df1 pd.DataFrame(np.…

leetcode 354. 俄罗斯套娃信封问题(dp+二分)

给定一些标记了宽度和高度的信封&#xff0c;宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候&#xff0c;这个信封就可以放进另一个信封里&#xff0c;如同俄罗斯套娃一样。 请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封&#…

fastlane use_legacy_build_api true

fastlane版本号&#xff1a;fastlane 1.108.0 Xcode版本号&#xff1a;8.1 MacOS版本号&#xff1a;10.12 使用fastlane打包 - Release / Ad-Hoc包时报错: [13:36:59]: There was an error exporting your application [13:36:59]: Unfortunately the new Xcode export API is …

获取所有权_住房所有权经济学深入研究

获取所有权Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seekin…

scala 单元测试_Scala中的法律测试简介

scala 单元测试Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples.基于财产的法律测试是scala生态系统中最强大的工具…

getBoundingClientRect说明

getBoundingClientRect用于获取某个元素相对于视窗的位置集合。 1.语法&#xff1a;这个方法没有参数。 rectObject object.getBoundingClientRect() 2.返回值类型&#xff1a;TextRectangle对象&#xff0c;每个矩形具有四个整数性质&#xff08; 上&#xff0c; 右 &#xf…

robot:接口入参为图片时如何发送请求

https://www.cnblogs.com/changyou615/p/8776507.html 接口是上传图片&#xff0c;通过F12抓包获得如下信息 由于使用的是RequestsLibrary&#xff0c;所以先看一下官网怎么传递二进制文件参数&#xff0c;https://2.python-requests.org//en/master/user/advanced/#post-multi…

开发小Tips-setValue

字典添加数据请用 [dict setValue:value forKey:key] 代替 [dict setObject:value forKey:key] 复制代码这样在一些网络传参时不用考虑nil的情况&#xff0c;?

浏览器快捷键指南_快速但完整的IndexedDB指南以及在浏览器中存储数据

浏览器快捷键指南Interested in learning JavaScript? Get my JavaScript ebook at jshandbook.com有兴趣学习JavaScript吗&#xff1f; 在jshandbook.com上获取我JavaScript电子书 IndexedDB简介 (Introduction to IndexedDB) IndexedDB is one of the storage capabilities …

Java-Character String StringBuffer StringBuilder

Java Character 类 Character 类用于对单个字符进行操作character 类在对象包装一个基本类型char的值 char ch "a";char uniChar \u039A;char[] charArray {a, b, c};使用Character的构造方法创建一个Character类对象 Character ch new Character(a);Charact…

已知两点坐标拾取怎么操作_已知的操作员学习-第3部分

已知两点坐标拾取怎么操作有关深层学习的FAU讲义 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as mu…