<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)308:二維區(qū)域和檢索 - 可變

          共 4177字,需瀏覽 9分鐘

           ·

          2021-07-04 09:08

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 二維區(qū)域和檢索 - 可變,我們先來看題面:
          https://leetcode-cn.com/problems/range-sum-query-2d-mutable/

          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).

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


          給你一個 2D 矩陣 matrix,請計算出從左上角 (row1, col1) 到右下角 (row2, col2) 組成的矩形中所有元素的和。

          上述紅色矩形框內(nèi)的,該矩形由左上角 (row1, col1) = (2, 1) 和右下角 (row2, col2) = (4, 3) 確定。其中,所包括的元素總和 sum = 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) -> 8
          update(3, 2, 2)
          sumRegion(2, 1, 4, 3) -> 10
           
          注意:
          矩陣 matrix 的值只能通過 update 函數(shù)來進(jìn)行修改
          你可以默認(rèn) update 函數(shù)和 sumRegion 函數(shù)的調(diào)用次數(shù)是均勻分布的
          你可以默認(rèn) row1 ≤ row2,col1 ≤ col2


          解題


          按照直接的求前n項和的思路來做這道題也可以過, 但是顯然這道題期望的并不是這樣的答案. 期望的應(yīng)該是二叉索引樹,。


          class NumMatrix {
          public:
              void add(int val, int x, int y)
              
          {
                  while(y < sum[x].size())
                  {
                      sum[x][y] += val;
                      y += (y&-y);
                  }
              }
              
              int getSum(int x, int y)
              
          {
                  int ans = 0;
                  while(y > 0)
                  {
                      ans += sum[x][y];
                      y -= (y&-y);
                  }
                  return ans;
              }
           
              NumMatrix(vector<vector<int>> &matrix):vec(matrix) {
                  if(matrix.size()==0) return;
                  int m = matrix.size(), n = matrix[0].size();
                  sum.resize(m, vector<int>(n+1, 0));
                  for(int i =0; i < m; i++)
                      for(int j =1; j <= n; j++)
                          add(matrix[i][j-1], i, j);
              }
           
              void update(int row, int col, int val) {
                  int d = val - vec[row][col];
                  vec[row][col] = val;
                  add(d, row, col+1);
              }
           
              int sumRegion(int row1, int col1, int row2, int col2) {
                  int ans = 0;
                  for(int i = row1; i <= row2; i++)
                      ans += (getSum(i, col2+1) - getSum(i, col1));
                  return ans;
              }
          private:
              vector<vector<int>> sum;
              vector<vector<int>> &vec;
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-280題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實戰(zhàn)301:刪除無效的括號

          LeetCode刷題實戰(zhàn)302:包含全部黑色像素的最小矩陣
          LeetCode刷題實戰(zhàn)303:區(qū)域和檢索 - 數(shù)組不可變
          LeetCode刷題實戰(zhàn)304:二維區(qū)域和檢索 - 矩陣不可變
          LeetCode刷題實戰(zhàn)305:島嶼數(shù)量II
          LeetCode刷題實戰(zhàn)306:累加數(shù)
          LeetCode刷題實戰(zhàn)307:區(qū)域和檢索 - 數(shù)組可修改

          瀏覽 36
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一道本无码免费视频 | a视频免费看 | 五月成人丁香 | 久久视频免费观看 | 伊人成人大香蕉网 |