LeetCode_Range Sum Query 2D - Immutable
2015-11-13 20:16
501 查看
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).
![](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
The above rectangle (with the red border) is defined by (row1, col1) =
(2, 1) and (row2, col2) = (4, 3), which contains sum =
8.
Example:
Note:
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.
解题思路:此题为二维求和,很容易想到先算出大的矩形和(0, 0, row2, col2),再减去两个侧面的矩形(0, 0, row2 - 1, 0) 和 (0, 0, 0, col2 - 1),再加上多减掉的小矩形和(0, 0, row1 - 1, col1 - 1)。但观察Note第二条可以发现,你的方法将被大量的调用,所以你需要在之前便将和准备好,即在类构造时,便将所有坐标到0的和计算出来。而我们可能面对的是这个二位数组非常大,所以死板的每次都计算每个坐标到(0, 0)的和也是不合适的,所以在求和时也要做对应的优化,即利用之前的计算来算更大面积的和,来减少求和导致的计算量。
下面推荐方法使用的思路为:分别计算(i - 1, j)、(i, j - 1)、(i - 1, j - 1)到(0, 0)的面积a,b,c。然后使用a+b-c+value(i, j)的方式求出和,并存放到原有的二位数组中。
推荐方法:
低俗方法:
col1) and lower right corner (row2, col2).
![](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
The 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) -> 8 sumRegion(1, 1, 2, 2) -> 11 sumRegion(1, 2, 2, 4) -> 12
Note:
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.
解题思路:此题为二维求和,很容易想到先算出大的矩形和(0, 0, row2, col2),再减去两个侧面的矩形(0, 0, row2 - 1, 0) 和 (0, 0, 0, col2 - 1),再加上多减掉的小矩形和(0, 0, row1 - 1, col1 - 1)。但观察Note第二条可以发现,你的方法将被大量的调用,所以你需要在之前便将和准备好,即在类构造时,便将所有坐标到0的和计算出来。而我们可能面对的是这个二位数组非常大,所以死板的每次都计算每个坐标到(0, 0)的和也是不合适的,所以在求和时也要做对应的优化,即利用之前的计算来算更大面积的和,来减少求和导致的计算量。
下面推荐方法使用的思路为:分别计算(i - 1, j)、(i, j - 1)、(i - 1, j - 1)到(0, 0)的面积a,b,c。然后使用a+b-c+value(i, j)的方式求出和,并存放到原有的二位数组中。
推荐方法:
public class NumMatrix { int matrix[][]; public NumMatrix(int[][] matrix) { this.matrix = matrix; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { int a = 0; if (i == 0) { a = 0; } else { a = matrix[i - 1][j]; } int b = 0; if (j == 0) { b = 0; } else { b = matrix[i][j - 1]; } int c = 0; if (i == 0 || j == 0) { c = 0; } else { c = matrix[i - 1][j - 1]; } matrix[i][j] = matrix[i][j] + a + b - c; } } } public int sumRegion(int row1, int col1, int row2, int col2) { int a, b, c; if (row1 == 0 || col1 == 0) { a = 0; } else { a = matrix[row1 - 1][col1 - 1]; } if (row1 == 0) { b = 0; } else { b = matrix[row1 - 1][col2]; } if (col1 == 0) { c = 0; } else { c = matrix[row2][col1 - 1]; } return matrix[row2][col2] + a - b - c; } public static void main(String[] args) { int[][] 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 } }; NumMatrixMe numMatrix = new NumMatrixMe(matrix); numMatrix.sumRegion(2, 1, 4, 3); numMatrix.sumRegion(1, 1, 2, 2); numMatrix.sumRegion(1, 2, 2, 4); numMatrix.sumRegion(0, 1, 0, 2); } }
低俗方法:
public class NumMatrixMe { private int[][] mMatrix = null; private int[][] mResults = null; // 15ms public NumMatrixMe(int[][] matrix) { mMatrix = matrix; if (mMatrix == null || mMatrix.length <= 0) { return; } int row = mMatrix.length; int col = mMatrix[0].length; mResults = new int[row][col]; for (int i = 0; i < mMatrix.length; i++) { for (int j = 0; j < mMatrix[0].length; j++) { if (i == 0 && j == 0) { mResults[i][j] = mMatrix[0][0]; } else { if (i > j) { int more = 0; for (int k = 0; k <= j; k++) { more = more + mMatrix[i][k]; } mResults[i][j] = mResults[i - 1][j] + more; } else if (i == j) { int more1 = 0; for (int k = 0; k <= j; k++) { more1 = more1 + mMatrix[i][k]; } int result1 = more1; int more2 = 0; for (int k = 0; k <= i; k++) { more2 = more2 + mMatrix[k][j]; } int result2 = more2; mResults[i][j] = mResults[i - 1][j - 1] + result1 + result2 - mMatrix[i][j]; } else if (i < j) { int more = 0; for (int k = 0; k <= i; k++) { more = more + mMatrix[k][j]; } mResults[i][j] = mResults[i][j - 1] + more; } } } } } public int sumRegion(int row1, int col1, int row2, int col2) { if (mMatrix == null || row1 > mMatrix.length || row2 > mMatrix.length || col1 > mMatrix[0].length || col2 > mMatrix[0].length) { return 0; } int result1 = mResults[row2][col2]; int result2 = 0; if (col1 >= 1) { result2 = mResults[row2][col1 - 1]; } int result3 = 0; if (row1 >= 1) { result3 = mResults[row1 - 1][col2]; } int result4 = 0; if (row1 >= 1 && col1 >= 1) { result4 = mResults[row1 - 1][col1 - 1]; } int result = result1 - result2 - result3 + result4; return result; } public static void main(String[] args) { int[][] 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 } }; NumMatrixMe numMatrix = new NumMatrixMe(matrix); numMatrix.sumRegion(2, 1, 4, 3); numMatrix.sumRegion(1, 1, 2, 2); numMatrix.sumRegion(1, 2, 2, 4); numMatrix.sumRegion(0, 1, 0, 2); } }
相关文章推荐
- easyui动态添加文本域
- UINavigationItem UINavigationBar分析
- [leetcode-304]Range Sum Query 2D - Immutable
- tableViewCell的不同高度创建
- UI第四天:事件处理
- 分别使用Jmeter/Loadrunner/SoapUI做Web Service接口测试的简单操作
- 问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the view outlet was not set.
- 111 AddressBookUI
- 根据输入的起止日期判断天数,strtotime,数据的去重和array_keys,array_values函数
- 用Fragment制作的Tab页面产生的UI重叠问题
- 【leetcode】【62】Unique Paths
- 安卓UI开发基础-View
- IE浏览器中 $.ajax返回uindefined 其他浏览器正常
- shuxue
- zuihao
- Testing Round #12 C Subsequences
- 导航条UINavigationBar和工具栏tabBar的灰色高亮线去掉
- [JS]EASYUI组件的程序基本结构
- UIDataPicker 时间选择器
- iOS8 UIAlertView键盘闪一下的问题