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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 將所有負元素移動到最后,并留出足夠的空間 > 原文: [https://www.geeksforgeeks.org/move-ve-elements-end-order-extra-space-allowed/](https://www.geeksforgeeks.org/move-ve-elements-end-order-extra-space-allowed/) 給定一個未排序的正負整數數組。 任務是將所有負元素放置在數組的末尾,而不更改正元素和負元素的順序。 **示例**: ``` Input : arr[] = {1, -1, 3, 2, -7, -5, 11, 6 } Output : 1 3 2 11 6 -1 -7 -5 Input : arr[] = {-5, 7, -3, -4, 9, 10, -1, 11} Output : 7 9 10 11 -5 -3 -4 -1 ``` 我們在下面的文章中討論了解決此問題的不同方法。 [使用恒定的額外空間重新排列正數和負數](https://www.geeksforgeeks.org/rearrange-positive-and-negative-numbers/) 如果允許我們使用額外的空間,問題將變得更加容易。 想法是創建一個空數組(`temp[]`)。 首先,我們存儲給定數組的所有正元素,然后將數組的所有負元素存儲在`Temp[]`中。 最后,我們將`temp[]`復制到原始數組。 以下是上述想法的實現: ## C/C++ ``` // C++ program to Move All -ve Element At End // Without changing order Of Array Element #include<bits/stdc++.h> using namespace std; // Moves all -ve element to end of array in // same order. void segregateElements(int arr[], int n) { ????// Create an empty array to store result ????int temp[n]; ????// Traversal array and store +ve element in ????// temp array ????int j = 0; // index of temp ????for (int i = 0; i < n ; i++) ????????if (arr[i] >= 0 ) ????????????temp[j++] = arr[i]; ????// If array contains all positive or all negative. ????if (j == n || j == 0) ????????return; ????// Store -ve element in temp array ????for (int i = 0 ; i < n ; i++) ????????if (arr[i] < 0) ????????????temp[j++] = arr[i]; ????// Copy contents of temp[] to arr[] ????memcpy(arr, temp, sizeof(temp)); } // Driver program int main() { ????int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 }; ????int n = sizeof(arr)/sizeof(arr[0]); ????segregateElements(arr, n); ????for (int i = 0; i < n; i++) ???????cout << arr[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>

                              哎呀哎呀视频在线观看