help~


  • 0
    W

    class NumMatrix {
    public:
    int** sum;
    int raw,col;
    NumMatrix(vector<vector<int>> matrix) {
    cout<<"---"<<endl;
    raw = matrix.size();
    cout << "+++++";
    if (raw == 0)
    return;
    col = matrix[0].size();
    sum = new int*[raw];
    for(int i = 0 ; i < raw ; i ++)
    sum[i] = new int[col];
    sum[0][0] = matrix[0][0];
    if(col != 1)
    {
    for (int i = 1; i < col ; i ++)
    sum[0][i] = sum[0][i-1] + matrix[0][i];
    }
    if(raw != 1)
    {
    for (int i = 1; i < raw ; i ++)
    sum[i][0] = sum[i-1][0] + matrix[i][0];
    }
    if (raw == 1 || col == 1)
    return;
    for (int i = 1 ; i < raw ; i ++)
    {
    for(int j = 1 ; j < col ; j ++)
    {
    sum[i][j] = sum[i][j-1] + sum[i-1][j] - sum[i - 1][j - 1] + matrix[i][j];
    }
    }
    cout <<sum[2][2];
    }

    int sumRegion(int row1, int col1, int row2, int col2) {
        if(raw == 0)
            return 0;
        else
        {
            if(row1 == 0 && col1 == 0)
                return sum[row2][col2];
            if(row1 == 0)
                return sum[row2][col2] - sum[row2][col - 1];
            if(col1 == 0)
                return sum[row2][col2] - sum[row1 -1][col2];
            return sum[row2][col2] - sum[row1 - 1][col2] - sum[row2][col1 - 1] + sum[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);

    here is my solution,but i cant even print "+++++"
    and it shows run time error ~
    is there any problem with my code?
    */


Log in to reply
 

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