<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)370:區(qū)間加法

          共 3063字,需瀏覽 7分鐘

           ·

          2021-09-02 16:57

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

          今天和大家聊的問題叫做 區(qū)間加法,我們先來看題面:
          https://leetcode-cn.com/problems/range-addition/

          Assume you have an array of length n initialized with all 0's and are given k update operations.


          Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.


          Return the modified array after all k operations were executed.


          假設(shè)你有一個長度為 n 的數(shù)組,初始情況下所有的數(shù)字均為 0,你將會被給出 k 個更新的操作。
          其中,每個操作會被表示為一個三元組:[startIndex, endIndex, inc],你需要將子數(shù)組 A[startIndex ... endIndex](包括 startIndex 和 endIndex)增加 inc。
          請你返回 k 次操作后的數(shù)組。

          示例


          示例:

          輸入: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
          輸出: [-2,0,3,5,3]

          解釋:

          初始狀態(tài):
          [0,0,0,0,0]

          進行了操作 [1,3,2] 后的狀態(tài):
          [0,2,2,2,0]

          進行了操作 [2,4,3] 后的狀態(tài):
          [0,2,5,5,3]

          進行了操作 [0,2,-2] 后的狀態(tài):
          [-2,0,3,5,3]


          解題


          創(chuàng)建一個 int[] 數(shù)組 ans,長度為 length。
          ??對于每一個給定的 [startIndex, endIndex, inc] 我們可以理解成如下:
          ??把 ans 的 [startIndex,length-1] 都加上了 inc,然后再把 [endIndex+1, length-1] 再減去 inc。
          ??具體的操作是:
          ??1、先 ans[startIndex] += val,ans[endIndex+1] += -val;
          ??2、然后 [startIndex,length-1] 遍歷進行 ans[i] += ans[i-1]。
          ??這樣做的目的是因為所有的三元組 [startIndex, endIndex, inc] 對數(shù)組 ans 的操作是獨立的,我們先對所有的三元組對 ans 的操作在邊界上做好,然后遍歷一遍 ans 數(shù)組即可同時執(zhí)行完所有三元組對 ans 的操作。

          class Solution {
             public int[] getModifiedArray(int length, int[][] updates) {
                  int[] ans = new int[length];
                  int start, end, val;
                  for (int[] update : updates) {
                      start = update[0];
                      end = update[1];
                      val = update[2];
                      ans[start] += val;
                      if (end < length - 1) {
                          ans[end + 1] -= val;
                      }
                  }
                  for (int i = 1; i < length; i++) {
                      ans[i] += ans[i - 1];
                  }
                  return ans;
              }
          }


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

          上期推文:

          LeetCode1-360題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)361:轟炸敵人
          LeetCode刷題實戰(zhàn)362:敲擊計數(shù)器
          LeetCode刷題實戰(zhàn)363:矩形區(qū)域不超過 K 的最大數(shù)值和
          LeetCode刷題實戰(zhàn)364:加權(quán)嵌套序列和 II
          LeetCode刷題實戰(zhàn)365:水壺問題
          LeetCode刷題實戰(zhàn)366:尋找二叉樹的葉子節(jié)點
          LeetCode刷題實戰(zhàn)367:有效的完全平方數(shù)
          LeetCode刷題實戰(zhàn)368:最大整除子集數(shù)
          LeetCode刷題實戰(zhàn)369:給單鏈表加一

          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  黄色做爱视频免费观看 | 大香蕉2020 | 女人20岁毛片60分钟 | 日本的一级黄色片 | 色婷婷在线视频观看 |