在 Java 中,如何將所有 0 移動到數組的末尾保留數組的順序? [2種方式]
已發表: 2020-12-31![在 Java 中,如何將所有 0 移動到數組的末尾保留數組的順序? [2種方式]](/uploads/article/578/Jg7tpgI3lsELLLkN.png)
我一直在解決在不同的採訪中以各種組合moving all 0's to end
的問題。 有時我要求將所有的 0 移到數組的前面,對沒有任何數據結構的數組進行排序等等。
在本教程中,我們將介紹移動所有 0 以結束保留數組順序的簡單示例。 有兩種方法。
方法-1)
QuickSort 分區邏輯。 什麼是樞軸點?
-
Pivot point
是快速排序算法中的一個關鍵元素。 它圍繞樞軸點執行和分區集合。 - 它將大於樞軸的元素排列在它之前,將大於樞軸的元素排列在它之後。
- 繼續循環以對數組進行排序
邏輯很簡單:
- 遍歷一個數組。
- 如果 array[i] 不等於 0,則將其與當前索引交換。
- 如果array[i] == 0,簡單地跳過循環
- 在我們的例子
0 is a Pivot point
。 - 每次我們找到 0,計數器樞軸將增加,元素將在樞軸點之前移動。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private static void approach1 ( int [ ] crunchifyData ) { // Move 0 to end of array int j = 0 ; for ( int i = 0 ; i < crunchifyData . length ; i ++ ) { if ( crunchifyData [ i ] ! = 0 ) { int temp = crunchifyData [ j ] ; crunchifyData [ j ] = crunchifyData [ i ] ; crunchifyData [ i ] = temp ; j ++ ; } } log ( "\n\nApproach-1 Result: " + Arrays . toString ( crunchifyData ) ) ; } |
方法-2)
- 創建一個相同大小的新數組
- 遍歷數組並跳過添加 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private static void approach2 ( int [ ] crunchifyData ) { int [ ] num = new int [ crunchifyData . length ] ; int j = 0 ; for ( int i = 0 ; i < crunchifyData . length ; i ++ ) { if ( crunchifyData [ i ] ! = 0 ) { num [ i - j ] = crunchifyData [ i ] ; } else { j ++ ; } } System . out . print ( "\n\nApproach-2 Result: " + Arrays . toString ( num ) ) ; } |
這是一個完整的程序:
CrunchifyMoveAll0ToEnd.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package crunchify . com . java . tutorials ; import java . util . Arrays ; /** * @author Crunchify.com * Requirement: Move all 0's to end of array preserving order * Input array: 8 8 3 0 4 0 6 0 9 1 * Output array: 8 8 3 4 6 9 1 0 0 0 */ public class CrunchifyMoveAll0ToEnd { public static void main ( String [ ] args ) { int [ ] crunchifyData ; crunchifyData = new int [ ] { 8 , 8 , 3 , 0 , 4 , 0 , 6 , 0 , 9 , 1 } ; log ( "Original array: " + Arrays . toString ( crunchifyData ) ) ; if ( crunchifyData == null | | crunchifyData . length == 0 ) { log ( "Empty Array" ) ; } approach1 ( crunchifyData ) ; approach2 ( crunchifyData ) ; } private static void approach1 ( int [ ] crunchifyData ) { // Move 0 to end of array int j = 0 ; for ( int i = 0 ; i < crunchifyData . length ; i ++ ) { if ( crunchifyData [ i ] ! = 0 ) { int temp = crunchifyData [ j ] ; crunchifyData [ j ] = crunchifyData [ i ] ; crunchifyData [ i ] = temp ; j ++ ; } } log ( "\n\nApproach-1 Result: " + Arrays . toString ( crunchifyData ) ) ; } // Create a new array with same size // Iterate through an Array and skip adding 0 private static void approach2 ( int [ ] crunchifyData ) { int [ ] num = new int [ crunchifyData . length ] ; int j = 0 ; for ( int i = 0 ; i < crunchifyData . length ; i ++ ) { if ( crunchifyData [ i ] ! = 0 ) { num [ i - j ] = crunchifyData [ i ] ; } else { j ++ ; } } System . out . print ( "\n\nApproach-2 Result: " + Arrays . toString ( num ) ) ; } private static void log ( String string ) { System . out . print ( string + " " ) ; } } |
Eclipse 控制台輸出:
1 2 3 4 5 6 |
Original array : [ 8 , 8 , 3 , 0 , 4 , 0 , 6 , 0 , 9 , 1 ] Approach - 1 Result : [ 8 , 8 , 3 , 4 , 6 , 9 , 1 , 0 , 0 , 0 ] Approach - 2 Result : [ 8 , 8 , 3 , 4 , 6 , 9 , 1 , 0 , 0 , 0 ] Process finished with exit code 0 |
如果您知道解決此問題的更好方法,請告訴我。 我很想听聽你的想法。
