304. Range Sum Query 2D - Immutable
Posted gopanama
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了304. Range Sum Query 2D - Immutable相关的知识,希望对你有一定的参考价值。
1 class NumMatrix { 2 int[][] sum; 3 4 public NumMatrix(int[][] matrix) { 5 int row = matrix.length; 6 if(row > 0){ 7 int col = matrix[0].length; 8 sum = new int[row+1][col+1]; 9 for(int i = 0; i < row; i++){ 10 for(int j = 0; j < col; j++){ 11 sum[i+1][j+1] = matrix[i][j] + sum[i][j+1] + sum[i+1][j] - sum[i][j]; 12 } 13 } 14 } 15 16 17 } 18 19 public int sumRegion(int row1, int col1, int row2, int col2) { 20 if(sum.length == 0) return 0; 21 if(sum[0].length == 0) return 0; 22 return sum[row2+1][col2+1] - sum[row2+1][col1] - sum[row1][col2+1] + sum[row1][col1]; 23 24 25 } 26 }
以上是关于304. Range Sum Query 2D - Immutable的主要内容,如果未能解决你的问题,请参考以下文章
算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable
算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable
[LeetCode] 304. Range Sum Query 2D - Immutable
LeetCode 304. Range Sum Query 2D - Immutable