您的位置:首页 > 产品设计 > UI/UE

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).



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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: