<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 用右側的最大元素替換每個元素 > 原文: [https://www.geeksforgeeks.org/replace-every-element-with-the-greatest-on-right-side/](https://www.geeksforgeeks.org/replace-every-element-with-the-greatest-on-right-side/) 給定一個整數數組,將每個元素替換為數組中的下一個最大元素(右側的最大元素)。 由于最后一個元素旁邊沒有元素,因此將其替換為 -1。 例如,如果數組為`{16, 17, 4, 4, 3, 5, 2}`,則應將其修改為`{17, 5, 5, 5, 2, -1}`。 ## 強烈建議您在繼續解決方案之前,單擊此處進行練習。 這個問題與本帖的[非常相似,解決方案也相似。](https://www.geeksforgeeks.org/leaders-in-an-array/) **樸素的方法**是要運行兩個循環。 外循環將從左到右一個接一個的拾取數組元素。 內循環將在選取的元素之后找到最大的元素。 最后,外循環將用內循環找到的最大元素替換選取的元素。 此方法的時間復雜度將為`O(n * n)`。 一種棘手的**方法**是使用數組的一次遍歷來替換所有元素。 這個想法是從最右邊的元素開始,一個一個地移到左側,并跟蹤最大的元素。 將每個元素替換為最大元素。 ## C++ ```cpp // C++ Program to replace every element with the greatest? // element on right side? #include <bits/stdc++.h> using namespace std; /* Function to replace every element with the? next greatest element */ void nextGreatest(int arr[], int size)? {? ????// Initialize the next greatest element? ????int max_from_right = arr[size-1];? ????// The next greatest element for the rightmost element? ????// is always -1? ????arr[size-1] = -1;? ????// Replace all other elements with the next greatest? ????for(int i = size-2; i >= 0; i--)? ????{? ????????// Store the current element (needed later for updating? ????????// the next greatest element)? ????????int temp = arr[i];? ????????// Replace current element with the next greatest? ????????arr[i] = max_from_right;? ????????// Update the greatest element, if needed? ????????if(max_from_right < temp)? ????????max_from_right = temp;? ????}? }? /* A utility Function that prints an array */ void printArray(int arr[], int size)? {? ????int i;? ????for (i = 0; i < size; i++)? ????????cout << arr[i] << " ";? ????cout << endl;? }? /* Driver program to test above function */ int main()? {? ????int arr[] = {16, 17, 4, 3, 5, 2};? ????int size = sizeof(arr)/sizeof(arr[0]);? ????nextGreatest (arr, size);? ????cout << "The modified array is: \n";? ????printArray (arr, size);? ????return (0);? }? // This is code is contributed by rathbhupendra ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看