[DP]304——Range Sum Query 2D - Immutable

Range Sum Query 2D - Immutable

思路:
思想和303差不多。變?yōu)橐粋€(gè)2D的表格。每個(gè)格子是從(0,0)到(i,j)矩形內(nèi)所有格子之和。動(dòng)態(tài)規(guī)劃,每次計(jì)算和的時(shí)候用上幾次計(jì)算的結(jié)果。時(shí)間復(fù)雜度為O(n)。

Runtime Error

class NumMatrix {

    int[][] sums;

    public NumMatrix(int[][] matrix) {
        if( matrix.length == 0) return;
        if(matrix[0].length == 0) return;
        int height = matrix.length;
        int width = matrix[0].length;
        sums = new int[height][width];
        sums[0][0] = matrix[0][0];
        for(int j = 1; j< width; j++){
            sums[0][j] = matrix[0][j] + sums[0][j-1];
        }
        for(int i = 1; i< height; i++){
            sums[i][0] = matrix[i][0] + sums[i-1][0];
        }
        for(int i = 1; i< height; i++){
            for (int j = 1; j< width; j++){
                sums[i][j] = sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1] + matrix[i][j];
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int temp = 0;
        if(row1 >= 1){
            if(col1 >= 1){
                temp = sums[row2][col2] - sums[row2][col1-1] - sums[row1-1][col2] + sums[row1-1][col1-1];
            }else{
                temp = sums[row2][col2] - sums[row1-1][col2];
            }
            
        }else{
            temp =  sums[row2][col2] - sums[row2][col1-1];
        }
        return temp;

    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

看來查詢nums也很占時(shí)間啊。。按照303改動(dòng)不行

class NumMatrix {

    int[][] sums;

    public NumMatrix(int[][] matrix) {
        if( matrix.length == 0) return;
        if(matrix[0].length == 0) return;
        int height = matrix.length;
        int width = matrix[0].length;
        sums =  matrix.clone();
        for(int j = 1; j< width; j++){
            sums[0][j] += sums[0][j-1];
        }
        for(int i = 1; i< height; i++){
            sums[i][0] += sums[i-1][0];
        }
        for(int i = 1; i< height; i++){
            for (int j = 1; j< width; j++){
                sums[i][j] += sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1];
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int temp = 0;
        if(row1 >= 1){
            if(col1 >= 1){
                temp = sums[row2][col2] - sums[row2][col1-1] - sums[row1-1][col2] + sums[row1-1][col1-1];
            }else{
                temp = sums[row2][col2] - sums[row1-1][col2];
            }
            
        }else{
            temp =  sums[row2][col2] - sums[row2][col1-1];
        }
        return temp;

    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

看了solution
大神代碼就是簡潔
空間復(fù)雜度O(1)
AC了終于

class NumMatrix {

    int[][] sums;

    public NumMatrix(int[][] matrix) {
        if( matrix.length == 0) return;
        if(matrix[0].length == 0) return;
        int height = matrix.length;
        int width = matrix[0].length;
        sums =  new int[height + 1][width +1];

        for(int i = 1; i< height+1; i++){
            for (int j = 1; j< width+1; j++){
                sums[i][j] = matrix[i-1][j-1] + sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1];
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return sums[row2+1][col2+1] - sums[row2+1][col1] - sums[row1][col2+1] + sums[row1][col1];
            
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,927評(píng)論 0 33
  • Range Sum Query - Immutable 思路:動(dòng)態(tài)規(guī)劃,每次計(jì)算和的時(shí)候用上一次計(jì)算的結(jié)果。時(shí)間復(fù)...
    Reflection_閱讀 287評(píng)論 0 0
  • Range Sum Query 2D - Immutable給定一個(gè)二維數(shù)組,求一個(gè)方塊內(nèi)所有元素之和((row1...
    Terence_F閱讀 408評(píng)論 0 0
  • 一場(chǎng)大雨瓢潑而來,透著股邪勁,大的沒個(gè)樣子,那一晚在夢(mèng)中被一陣緊似一陣的豪雨驚醒,感覺到天就要隨時(shí)四分五裂,砸個(gè)滿...
    碎片碎碎念閱讀 407評(píng)論 0 0
  • 他用熾熱的星光,在寂寞的夜里寫下詩行。 他用深邃的眼神,在漫長的心間孤獨(dú)眺望。 他在等待一陣風(fēng)的親吻,他在期盼一朵...
    博川先生閱讀 220評(píng)論 0 2

友情鏈接更多精彩內(nèi)容