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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 將所有零移動到數組末尾 > 原文: [https://www.geeksforgeeks.org/move-zeroes-end-array/](https://www.geeksforgeeks.org/move-zeroes-end-array/) 給定一個隨機數數組,將給定數組的所有零都推到該數組的末尾。 例如,如果給定數組為`{1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}`,則應將其更改為`{1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}`。 所有其他元素的順序應相同。 預期時間復雜度為`O(n)`,額外空間為`O(1)`。 **示例**: ``` Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0}; Output : arr[] = {1, 2, 4, 3, 5, 0, 0}; Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0}; ``` 有很多方法可以解決此問題。 以下是解決此問題的簡單有趣的方法。 從左到右遍歷給定的數組“`arr`”。 遍歷時,維護數組中非零元素的數量。 讓計數為“計數”。 對于每個非零元素`arr[i]`,將其放在“`arr[count]`”處,并遞增“`count`”。 完全遍歷之后,所有非零元素都已移至前端,并且“`count`”被設置為前 0 的索引。現在我們要做的就是運行一個循環,使所有元素從“`count`”到結束都為零。 的數組。 下面是上述方法的實現。 ## C++ ```cpp // A C++ program to move all zeroes at the end of array #include <iostream> using namespace std; // Function which pushes all zeros to end of an array. void pushZerosToEnd(int arr[], int n) { ????int count = 0;? // Count of non-zero elements ????// Traverse the array. If element encountered is non- ????// zero, then replace the element at index 'count'? ????// with this element ????for (int i = 0; i < n; i++) ????????if (arr[i] != 0) ????????????arr[count++] = arr[i]; // here count is? ???????????????????????????????????// incremented ????// Now all non-zero elements have been shifted to? ????// front and? 'count' is set as index of first 0.? ????// Make all elements 0 from count to end. ????while (count < n) ????????arr[count++] = 0; } // Driver program to test above function int main() { ????int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; ????int n = sizeof(arr) / sizeof(arr[0]); ????pushZerosToEnd(arr, n); ????cout << "Array after pushing all zeros to end of array :\n"; ????for (int i = 0; i < n; i++) ????????cout << arr[i] << " "; ????return 0; } ``` ## Java ```java /* Java program to push zeroes to back of array */ import java.io.*; class PushZero { ????// Function which pushes all zeros to end of an array. ????static void pushZerosToEnd(int arr[], int n) ????{ ????????int count = 0;? // Count of non-zero elements ????????// Traverse the array. If element encountered is ????????// non-zero, then replace the element at index 'count' ????????// with this element ????????for (int i = 0; i < n; i++) ????????????if (arr[i] != 0) ????????????????arr[count++] = arr[i]; // here count is ???????????????????????????????????????// incremented ????????// Now all non-zero elements have been shifted to ????????// front and 'count' is set as index of first 0\. ????????// Make all elements 0 from count to end. ????????while (count < n) ????????????arr[count++] = 0; ????} ????/*Driver function to check for above functions*/ ????public static void main (String[] args) ????{ ????????int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; ????????int n = arr.length; ????????pushZerosToEnd(arr, n); ????????System.out.println("Array after pushing zeros to the back: "); ????????for (int i=0; i<n; i++) ????????????System.out.print(arr[i]+" "); ????} } /* This code is contributed by Devesh Agrawal */ ``` ## Python3 ```py # Python3 code to move all zeroes # at the end of array # Function which pushes all # zeros to end of an array. def pushZerosToEnd(arr, n): ????count = 0 # Count of non-zero elements ????# Traverse the array. If element? ????# encountered is non-zero, then ????# replace the element at index ????# 'count' with this element ????for i in range(n): ????????if arr[i] != 0: ????????????# here count is incremented ????????????arr[count] = arr[i] ????????????count+=1 ????# Now all non-zero elements have been ????# shifted to front and 'count' is set ????# as index of first 0\. Make all? ????# elements 0 from count to end. ????while count < n: ????????arr[count] = 0 ????????count += 1 # Driver code arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] n = len(arr) pushZerosToEnd(arr, n) print("Array after pushing all zeros to end of array:") print(arr) # This code is contributed by "Abhishek Sharma 44" ``` ## C# ```cs /* C# program to push zeroes to back of array */ using System; class PushZero { ????// Function which pushes all zeros? ????// to end of an array. ????static void pushZerosToEnd(int []arr, int n) ????{ ????????// Count of non-zero elements ????????int count = 0;? ????????// Traverse the array. If element encountered is ????????// non-zero, then replace the element? ????????// at index a..counta.. with this element ????????for (int i = 0; i < n; i++) ????????if (arr[i] != 0) ????????// here count is incremented ????????arr[count++] = arr[i];? ????????// Now all non-zero elements have been shifted to ????????// front and a..counta.. is set as index of first 0\. ????????// Make all elements 0 from count to end. ????????while (count < n) ????????arr[count++] = 0; ????} ????// Driver function? ????public static void Main () ????{ ????????int []arr = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9}; ????????int n = arr.Length; ????????pushZerosToEnd(arr, n); ????????Console.WriteLine("Array after pushing all zeros to the back: "); ????????for (int i = 0; i < n; i++) ????????Console.Write(arr[i] +" "); ????} } /* This code is contributed by Anant Agrawal */ ``` ## PHP ```php <?php? // A PHP program to move all // zeroes at the end of array // Function which pushes all? // zeros to end of an array. function pushZerosToEnd(&$arr, $n) { ????// Count of non-zero elements ????$count = 0;? ????// Traverse the array. If? ????// element encountered is ????// non-zero, then replace? ????// the element at index? ????// 'count' with this element ????for ($i = 0; $i < $n; $i++) ????????if ($arr[$i] != 0) ????????????// here count is incremented ????????????$arr[$count++] = $arr[$i];? ????// Now all non-zero elements? ????// have been shifted to front ????// and 'count' is set as index?? ????// of first 0\. Make all elements ????// 0 from count to end. ????while ($count < $n) ????????$arr[$count++] = 0; } // Driver Code $arr = array(1, 9, 8, 4, 0, 0, ?????????????2, 7, 0, 6, 0, 9); $n = sizeof($arr); pushZerosToEnd($arr, $n); echo "Array after pushing all " . ?????"zeros to end of array :\n"; for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; // This code is contributed? // by ChitraNayal ?> ``` **輸出**: ``` Array after pushing all zeros to end of array : 1 9 8 4 2 7 6 9 0 0 0 0 ``` **時間復雜度**:`O(n)`,其中`n`是輸入數組中的元素數。 **輔助空間**:`O(1)` 本文由 [**Chandra Prakash**](https://www.facebook.com/chandra.prakash.52643?fref=ts) 貢獻。 如果發現任何不正確的地方,或者想分享有關上述主題的更多信息,請寫評論。
                  <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>

                              哎呀哎呀视频在线观看