領扣(LeetCode)二維地區和檢索 個人題解

來源:互聯網
上載者:User

標籤:集合   題解   代碼   轉換   images   可變   時間   樣本   alt   

給定一個二維矩陣,計算其子矩形範圍內元素的總和,該子矩陣的左上方為 (row1, col1) ,右下角為 (row2, col2)。


子矩陣左上方 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),該子矩形內元素的總和為 8。

樣本:

給定 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) -> 8sumRegion(1, 1, 2, 2) -> 11sumRegion(1, 2, 2, 4) -> 12

說明:

  1. 你可以假設矩陣不可變。
  2. 會多次調用 sumRegion 方法
  3. 你可以假設 row1 ≤ row2 且 col1 ≤ col2。

 

對於這一題,一開始拿到以為是很簡單的,還想著為什麼會放在中等題裡面。

雖然知道可能會出現逾時的問題,但是第一次寫還是嘗試了暴力遍曆求地區內值的和。答案雖然是正確的,當然顯而易見報錯了。逾時。

後來想到了(其實還是參考了思路,拜託欸,我可是萌新,哪裡接觸過這種空間換時間的神奇操作 XD),其實,每個地區塊到左上方的值都可以簡化為上一個已經處理過的地區塊的加減乘除的取值,然後加上當前的值。同時,某個子領域的塊也能轉換成以上塊的加減乘除集合。

公式為:

(略)

代碼如下:

class NumMatrix {    int[][] matrix;    int[][] markmatr;            public NumMatrix(int[][] matrix) {        this.matrix=matrix;        int x=matrix.length;        int y=x>0?matrix[0].length:0;        markmatr=new int[x+1][y+1];        for(int i=1;i<=x;i++)        {            for(int j=1;j<=y;j++)            {                markmatr[i][j]=markmatr[i-1][j]+markmatr[i][j-1]-markmatr[i-1][j-1]+matrix[i-1][j-1];            }        }    }    public int sumRegion(int row1, int col1, int row2, int col2) {        int sum=0;        sum=markmatr[row2+1][col2+1]-markmatr[row1][col2+1]-markmatr[row2+1][col1]+markmatr[row1][col1];        return sum;    }}

 

領扣(LeetCode)二維地區和檢索 個人題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.