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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 根據給定的索引對數組重新排序 > 原文: [https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/](https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/) 給定兩個大小相同的整數數組“`arr[]`”和“`index[]`”,請根據給定的索引數組對“`arr[]`”中的元素進行重新排序。 不允許指定數組`arr`的長度。 **示例**: ``` Input: arr[] = [10, 11, 12]; index[] = [1, 0, 2]; Output: arr[] = [11, 10, 12] index[] = [0, 1, 2] Input: arr[] = [50, 40, 70, 60, 90] index[] = [3, 0, 4, 1, 2] Output: arr[] = [40, 60, 90, 50, 70] index[] = [0, 1, 2, 3, 4] ``` 預期時間復雜度`O(n)`和輔助空間`O(1)` **我們強烈建議您最小化瀏覽器,然后自己嘗試。** **簡單解決方案**是使用與給定數組大小相同的輔助數組`temp[]`。 遍歷給定的數組,并使用`index[]`將所有元素放在`temp[]`中的正確位置。 最后,將`temp[]`復制到`arr[]`并將`index[i]`的所有值設置為`i`。 ## C++ ```cpp // C++ program to sort an array according to given // indexes #include<iostream> using namespace std; // Function to reorder elements of arr[] according // to index[] void reorder(int arr[], int index[], int n) { ????int temp[n]; ????// arr[i] should be present at index[i] index ????for (int i=0; i<n; i++) ????????temp[index[i]] = arr[i]; ????// Copy temp[] to arr[] ????for (int i=0; i<n; i++) ????{? ???????arr[i]?? = temp[i]; ???????index[i] = i; ????} } // Driver program int main() { ????int arr[] = {50, 40, 70, 60, 90}; ????int index[] = {3,? 0,? 4,? 1,? 2}; ????int n = sizeof(arr)/sizeof(arr[0]); ????reorder(arr, index, n); ????cout << "Reordered array is: \n"; ????for (int i=0; i<n; i++) ????????cout << arr[i] << " "; ????cout << "\nModified Index array is: \n"; ????for (int i=0; i<n; i++) ????????cout << index[i] << " "; ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看