国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode -- Range Sum Query 2D - Immutable

2019-11-08 02:11:30
字體:
來源:轉載
供稿:網友
題目描述:Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).Range Sum Query 2DThe above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.Example:Given 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) -> 8sumRegion(1, 1, 2, 2) -> 11sumRegion(1, 2, 2, 4) -> 12Note:You may assume that the matrix does not change.There are many calls to sumRegion function.You may assume that row1 ≤ row2 and col1 ≤ col2.給定一個矩陣,傳入(x1,y1) (x2,y2),返回兩坐標構成的矩形中包含的數字之和。思路:在初始化矩陣時,緩存每個坐標到原點構成的面積(數字之和)。這樣多次調用對性能的影響是冪等的。面積計算時,找出坐標面積之間的關系進行計算即可:設左上坐標為 p0(0,0) ,坐標1 p1(x1,y1),坐標2 p2(x2, y2),并假設p2在p1的右下方(橫縱坐標大于等于)。S = S[x2,y2](總面積) - S[x1-1,y2](x1左側面積) - S[x2, y1-1](y1上面面積) + S(x1-1,y1-1)(多減去的部分)實現代碼:
public class NumMatrix {        PRivate int[,] _matrix;        private int[,] _sum;        public NumMatrix(int[,] matrix) {            _matrix = matrix;    		CreateCache();        }            public int SumRegion(int row1, int col1, int row2, int col2) {    		// get the all    		var areaTotal = _sum[row2, col2];     		    		// remove the left side sum    		var areaLeft = col1 > 0 ? _sum[row2, col1-1] : 0;    		// remove the top side sum    		var areaTop = row1 > 0 ? _sum[row1-1, col2] : 0;    		    		var result = areaTotal - areaLeft - areaTop;    		// check if there is overlay between top area and left area    		if(row1 > 0 && col1 > 0){    			var common = _sum[row1-1,col1-1];    			result += common;    		}    		return result;        }    	    	private void CreateCache(){    		var rows = _matrix.GetLength(0);    		var columns = _matrix.GetLength(1);    		if(rows == 0 && columns == 0){    			return ;    		}    		    		    		_sum = new int[rows,columns];    		    		// init the first row and column value    		_sum[0,0] = _matrix[0,0];    		for (var i = 1;i < rows;i ++){    			_sum[i,0] = _matrix[i,0] + _sum[i-1,0];    		}    		for (var j = 1;j < columns; j++){    			_sum[0,j] = _matrix[0,j] + _sum[0, j-1];    		}    		    		// from top to bottom , left to right    		// cache the sum value of each rect    		for (var i = 1;i < rows; i++){    			for (var j = 1;j < columns; j++){    				_sum[i,j] = _matrix[i,j]+ _sum[i,j-1] + _sum[i-1, j] - _sum[i-1,j-1];    			}    		}    		    	//	Console.WriteLine(_sum);    	}}// Your NumMatrix object will be instantiated and called as such:// NumMatrix numMatrix = new NumMatrix(matrix);// numMatrix.SumRegion(0, 1, 2, 3);// numMatrix.SumRegion(1, 2, 3, 4);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普洱| 军事| 洛川县| 张掖市| 铁岭市| 揭东县| 元朗区| 福泉市| 宁陕县| 北辰区| 水富县| 龙游县| 横峰县| 河池市| 城步| 仲巴县| 景洪市| 左权县| 丁青县| 浠水县| 尚志市| 山西省| 香河县| 曲阜市| 额济纳旗| 黄石市| 砚山县| 惠州市| 兴文县| 固原市| 碌曲县| 鄂温| 承德市| 台北县| 井冈山市| 民丰县| 平度市| 武定县| 高清| 会泽县| 新蔡县|