在 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 |
如果您知道解决此问题的更好方法,请告诉我。 我很想听听你的想法。
