Skip to content

Commit b94df4a

Browse files
author
wangpeng
committed
feat(MEDIUM): 304_NumMatrix
1 parent f27ed13 commit b94df4a

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package pp.arithmetic.leetcode;
2+
3+
/**
4+
* Created by wangpeng on 2019-04-02.
5+
* 304. 二维区域和检索 - 矩阵不可变
6+
* <p>
7+
* 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
8+
* <image src="https://leetcode.com/static/images/courses/range_sum_query_2d.png"></image>
9+
* <p>
10+
* Range Sum Query 2D
11+
* 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。
12+
* <p>
13+
* 示例:
14+
* <p>
15+
* 给定 matrix = [
16+
* [3, 0, 1, 4, 2],
17+
* [5, 6, 3, 2, 1],
18+
* [1, 2, 0, 1, 5],
19+
* [4, 1, 0, 1, 7],
20+
* [1, 0, 3, 0, 5]
21+
* ]
22+
* <p>
23+
* sumRegion(2, 1, 4, 3) -> 8
24+
* sumRegion(1, 1, 2, 2) -> 11
25+
* sumRegion(1, 2, 2, 4) -> 12
26+
* 说明:
27+
* <p>
28+
* 你可以假设矩阵不可变。
29+
* 会多次调用 sumRegion 方法。
30+
* 你可以假设 row1 ≤ row2 且 col1 ≤ col2。
31+
*
32+
* @see <a href="https://leetcode-cn.com/problems/range-sum-query-2d-immutable/">range-sum-query-2d-immutable</a>
33+
*/
34+
public class _304_NumMatrix {
35+
36+
public static void main(String[] args) {
37+
NumMatrix numMatrix = new NumMatrix(new int[][]{
38+
{-4,-5}
39+
});
40+
System.out.println(numMatrix.sumRegion(0, 1, 0, 1));
41+
// System.out.println(numMatrix.sumRegion(1, 1, 2, 2));
42+
// System.out.println(numMatrix.sumRegion(1, 2, 2, 4));
43+
}
44+
45+
/**
46+
* 由于sumRegion方法会被方法调用,所以计算的话不能每次都遍历
47+
* 基于动态规划的思想,将矩阵中的每一点相对于(0,0)初始化计算出来
48+
* DP[i][j]=DP[i-1][j]+DP[i][j-1]-DP[i-1][j-1],其中i>0,j>0
49+
* 已知:左上角 (row1, col1),右下角(row2, col2)
50+
* 根据题目可以得出面积=DP[row2][col2]-DP[row1-1][col2]-DP[row2][col1-1]+DP[row1-1][col1-1],其中r1,c1必须大于0
51+
* 如r1==0或者c1==0做特殊处理
52+
*/
53+
public static class NumMatrix {
54+
55+
private int[][] dp;
56+
57+
public NumMatrix(int[][] matrix) {
58+
if (matrix.length == 0) return;
59+
dp = new int[matrix.length][matrix[0].length];
60+
//初始化第一行和第一列
61+
dp[0][0] = matrix[0][0];
62+
for (int i = 1; i < matrix.length; i++) {
63+
dp[i][0] = dp[i - 1][0] + matrix[i][0];
64+
}
65+
for (int i = 1; i < matrix[0].length; i++) {
66+
dp[0][i] = dp[0][i - 1] + matrix[0][i];
67+
}
68+
for (int i = 1; i < matrix.length; i++) {
69+
for (int j = 1; j < matrix[0].length; j++) {
70+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + matrix[i][j];
71+
}
72+
}
73+
}
74+
75+
public int sumRegion(int row1, int col1, int row2, int col2) {
76+
if (dp == null || dp.length == 0) return 0;
77+
if (row1 == 0 && col1 == 0) {
78+
return dp[row2][col2];
79+
} else if (row1 > 0 && col1 > 0) {
80+
return dp[row2][col2] - dp[row1 - 1][col2] - dp[row2][col1 - 1] + dp[row1 - 1][col1 - 1];
81+
} else if (row1 == 0) {
82+
return dp[row2][col2] - dp[row2][col1 - 1];
83+
} else {
84+
return dp[row2][col2] - dp[row1 - 1][col2];
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)