c++ solution using simple dp...easy to understand


  • 0
    D
    int a[1000][1000];
        int flag=0;
        NumMatrix(vector<vector<int>> &matrix) {
            int r=matrix.size();
            if(r==0)
            flag=1;
            if(flag==0)
            {
            int c=matrix[0].size();
            int i,j;
            memset(a,0,sizeof a);
            for(i=1;i<=r;i++)
            {
                for(j=1;j<=c;j++)
                {
                    a[i][j]=a[i-1][j]+a[i][j-1]+matrix[i-1][j-1]-a[i-1][j-1];
                }
              }
            }
        }
    
        int sumRegion(int row1, int col1, int row2, int col2) {
            if(flag==1)
            return 0;
            int ans=a[row2+1][col2+1]+a[row1][col1]-a[row1][col2+1]-a[row2+1][col1];
            return ans;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.