GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/NumMatrix.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathNumMatrix.javaMore file actions 34 lines (27 loc) · 943 Bytes / Copy pathNumMatrix.javaFile metadata and controls34 lines (27 loc) · 943 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /** * https://leetcode.com/articles/range-sum-query-2d-immutable/ */ public class NumMatrix { private int[][] dp; public NumMatrix(int[][] matrix) { if (matrix.length == 0) { return; } int row = matrix.length; int col = matrix[0].length; dp = new int[row + 1][col + 1]; for (int i = 1; i <= row; i++) { for (int j = 1; j <= col; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + matrix[i - 1][j - 1]; } } } public int sumRegion(int row1, int col1, int row2, int col2) { return dp[row2 + 1][col2 + 1] - dp[row2 + 1][col1] - dp[row1][col2 + 1] + dp[row1][col1]; } } // Your NumMatrix object will be instantiated and called as such: // NumMatrix numMatrix = new NumMatrix(matrix); // numMatrix.sumRegion(0, 1, 2, 3); // numMatrix.update(1, 1, 10); // numMatrix.sumRegion(1, 2, 3, 4); |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |