<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國際加速解決方案。 廣告
                # 將數組中的 0 和 1 分開 > 原文: [https://www.geeksforgeeks.org/segregate-0s-and-1s-in-an-array-by-traversing-array-once/](https://www.geeksforgeeks.org/segregate-0s-and-1s-in-an-array-by-traversing-array-once/) 系統會以隨機順序為您提供 0 和 1 的數組。 分隔數組左側的 0 和右側的 1。 遍歷數組僅一次。 ``` Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] ``` **方法 1(計數 0s 或 1s)** 感謝 Naveen 建議使用此方法。 1)計算 0 的數量。 令數量為`C`。 2)一旦有了計數,就可以在數組的`C`位置的開頭放置 0,在其余的`n – C`位置放置 1。 **時間復雜度**: `O(n)` ## C++ ```cpp // C++ code to Segregate 0s and 1s in an array #include <bits/stdc++.h> using namespace std; // Function to segregate 0s and 1s void segregate0and1(int arr[], int n) { ????int count = 0; // Counts the no of zeros in arr ????for (int i = 0; i < n; i++) { ????????if (arr[i] == 0) ????????????count++; ????} ????// Loop fills the arr with 0 until count ????for (int i = 0; i < count; i++) ????????arr[i] = 0; ????// Loop fills remaining arr space with 1 ????for (int i = count; i < n; i++) ????????arr[i] = 1; } // Function to print segregated array void print(int arr[], int n) { ????cout << "Array after segregation is "; ????for (int i = 0; i < n; i++) ????????cout << arr[i] << " "; } // Driver function int main() { ????int arr[] = { 0, 1, 0, 1, 1, 1 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????segregate0and1(arr, n); ????print(arr, n); ????return 0; } // This code is contributed by Sahil_Bansall ```
                  <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>

                              哎呀哎呀视频在线观看