Range Sum Query 2D - Immutable My Submissionsstion-leetcode

这道题目承接了上面一题。只是由二维变换成为了三维。这里却由衷地侥幸,因为我尽管绝得我数学学的不好,但是我还是收到了概率论中求概率模型的启发而做了出来。

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:
Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
Note:
You may assume that the matrix does not change.
There are many calls to sumRegion function.
You may assume that row1 ≤ row2 and col1 ≤ col2.

public class NumMatrix {
    //10:00-10:50
    int width;
    int hight;
    int [][]sum;
    public NumMatrix(int[][] matrix) {
        hight=matrix.length;
        if(hight!=0){
                    width=matrix[0].length;//get the marix width and hight;

        }


        //make a 2D sum matrix to log all the sum of matrix from (1,1) to maxWidth and maxHight

        //but the main problem is the sequence to cover all the area
        sum=new int [hight+1][width+1];//0000
        for(int i=1;i<=hight;i++){
            for(int j=1;j<=width;j++){
                /*if(i==0&&j==0){//start point 
                    sum[i][j]=matrix[i][j];
                }else if(i==0&&j!=0)
                {
                    sum[i][j]=matrix[i][j]+sum[i][j-1];//align to the buttom wall
                }else if(j==0&&i!=0){
                    sum[i][j]=matrix[i][j]+sum[i-1][j];//align to the left wall
                }*/
                sum[i][j]=matrix[i-1][j-1]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
            }
        }
    }
   /* public static void main(String[] args){
        int [][] matrix = {

                [3, 0, 1, 4, 2],
                  [5, 6, 3, 2, 1],
                  [1, 2, 0, 1, 5],
                  [4, 1, 0, 1, 7],
                  [1, 0, 3, 0, 5]
                                     {3, 0, 1, 4, 2},{5, 6, 3, 2, 1}, {1, 2, 0, 1, 5}, {4, 1, 0, 1, 7},
                                     {1, 0, 3, 0, 5}




        };
        NumMatrix numMatrix=new NumMatrix(matrix);
        //System.out.println(numMatrix.sumRegion(2, 1, 4, 3));
        System.out.println(numMatrix.sumRegion(1, 1, 2, 2));

    }
    */
    public int sumRegion(int row1, int col1, int row2, int col2) {//这里不可以按照字面的意思去理解,横着的就是row,竖着的就是colom,要看数值
       // int i1=row1-1;
      //  int j1=col1-1;
       // int i2=row2-1;
       // int j2=col2-1;//coverse traditional to matrix in pc
        int result;

        int i1=row1+1;
        int j1=col1+1;
        int i2=row2+1;
        int j2=col2+1;

        if(width==0||hight==0){
            return 0;
        }
       /* int i1=col1;
        int j1=row1;
        int i2=col2;
        int j2=row2;*/
       /* if(i1==0&&j1==0){//start point 
             result=sum[i2][j2];
         }else if(i1==0&&j1!=0) {
            result= sum[i2][j2]-sum[i2][j1-1];//align to the buttom wall
         }else if(j1==0&&i1!=0){
             result= sum[i2][j2]-sum[i1-1][j2];//align to the left wall
        }
        */

        result= sum[i2][j2]-sum[i2][j1-1]-sum[i1-1][j2]+sum[i1-1][j1-1];

        return  result;

    }
}


// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);

昨天晚上做了将近两个小时,思路基本正确但是很多的细节都已经没有大脑去思考了。
早上半个小时搞定。

这里面有几个总结:
1. 数学在变成里面可以启迪思路。数学可以给人自信。
2. 把题目尽量地读完整。

这道题目里面有一个巧合,就是当你把横纵坐标相互颠倒的时候得到的第一个用例的结果是巧合的相同。以至于我一错到底。

  1. 将数组的边界留出零元素来保证一致性比if判断更加简洁方便,但是一定要想清楚是怎样加一减一。

  2. 出现accpted的时候,心中是激动的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小马工匠坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值