Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NumMatrix {
- public:
- vector<vector<int>> grid ;
- int valid (int r , int c)
- {
- return r < grid.size() && c < grid[0].size() ? grid[r][c] : 0;
- }
- NumMatrix(vector<vector<int>>& matrix) {
- grid = matrix;
- for (int i = 0 ; i < grid.size() ; i ++ )
- for (int j = 0 ; j < grid[0].size() ; j ++)
- grid[i][j] += valid (i-1, j) + valid(i,j-1) - valid(i-1,j-1);
- }
- int sumRegion(int row1, int col1, int row2, int col2) {
- return valid(row2, col2) - valid(row1 - 1, col2) - valid(row2 , col1 - 1) + valid (row1 -1, col1 -1);
- }
- };
- /**
- * Your NumMatrix object will be instantiated and called as such:
- * NumMatrix* obj = new NumMatrix(matrix);
- * int param_1 = obj->sumRegion(row1,col1,row2,col2);
- */
- /*
- */
Add Comment
Please, Sign In to add comment