SalmaYasser

Untitled

Nov 10th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. class NumMatrix {
  2. public:
  3. vector<vector<int>> grid ;
  4.  
  5. int valid (int r , int c)
  6. {
  7. return r < grid.size() && c < grid[0].size() ? grid[r][c] : 0;
  8. }
  9. NumMatrix(vector<vector<int>>& matrix) {
  10.  
  11. grid = matrix;
  12.  
  13. for (int i = 0 ; i < grid.size() ; i ++ )
  14. for (int j = 0 ; j < grid[0].size() ; j ++)
  15. grid[i][j] += valid (i-1, j) + valid(i,j-1) - valid(i-1,j-1);
  16. }
  17.  
  18. int sumRegion(int row1, int col1, int row2, int col2) {
  19.  
  20.  
  21. return valid(row2, col2) - valid(row1 - 1, col2) - valid(row2 , col1 - 1) + valid (row1 -1, col1 -1);
  22.  
  23. }
  24. };
  25.  
  26. /**
  27. * Your NumMatrix object will be instantiated and called as such:
  28. * NumMatrix* obj = new NumMatrix(matrix);
  29. * int param_1 = obj->sumRegion(row1,col1,row2,col2);
  30. */
  31.  
  32. /*
  33.  
  34.  
  35.  
  36.  
  37. */
Add Comment
Please, Sign In to add comment