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.pswp.cn/news/391681.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391681.shtml
英文地址,請注明出處:http://en.pswp.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…