Range Sum Query 2D - Immutable
来源:互联网 发布:如何查询淘宝买家等级 编辑:程序博客网 时间:2024/06/05 02:51
这道题一定要注意下标,一开始自己做的全都没有下标-1这一操作。
public class NumMatrix { int[][] sum; public NumMatrix(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return; } int row = matrix.length; int col = matrix[0].length; sum = new int[row][col]; for (int i = 0; i < row; i++) { if (i == 0) { sum[i][0] = matrix[0][0]; } else { sum[i][0] = matrix[i][0] + sum[i - 1][0]; } } for (int i = 1; i < col; i++) { sum[0][i] = matrix[0][i] + sum[0][i - 1]; } for (int i = 1; i < row; i++) { for (int j = 1; j < col; j++) { sum[i][j] = matrix[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } } public int sumRegion(int row1, int col1, int row2, int col2) { if (sum == null) { return 0; } if (row1 == 0 && col1 == 0) { return sum[row2][col2]; } else if (row1 == 0) { return sum[row2][col2] - sum[row2][col1 - 1]; } else if (col1 == 0) { return sum[row2][col2] - sum[row1 - 1][col2]; } else { return sum[row2][col2] - sum[row2][col1 - 1] - sum[row1 - 1][col2] + sum[row1 - 1][col1 - 1]; } }}// 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);
0 0
- Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- [Leetcode]Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- Leetcode Range Sum Query 2D - Immutable
- LeetCode Range Sum Query 2D - Immutable
- [Leetcode]Range Sum Query 2D - Immutable
- Leetcode194: Range Sum Query 2D - Immutable
- Leetcode Range Sum Query 2D - Immutable
- Leetcode: Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
- Leetcode: Range Sum Query 2D - Immutable
- LeetCode304. Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- leetcode:Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- LeetCode|Range Sum Query 2D - Immutable
- 304. Range Sum Query 2D - Immutable
- jsp 路径问题
- 关于字符集的若干问题总结
- 字符集典型场景
- mex 1 hello,world
- 一些站外很有价值的博客
- Range Sum Query 2D - Immutable
- mex2 Inputs and Outputs
- Spring MVC controller带参数跳转
- test
- TensorLayer : 最新深度学习与增强学习库
- 数据库SQL 优化
- OpenJ_Bailian 2755 神奇的口袋
- HDU 1180 诡异的楼梯(BFS)
- 线程 —— 定时器介绍