In Java come spostare tutti gli 0 alla fine dell'ordine di conservazione dell'array di un array? [2 modi]
Pubblicato: 2020-12-31![In Java come spostare tutti gli 0 alla fine dell'ordine di conservazione dell'array di un array? [2 modi]](/uploads/article/578/Jg7tpgI3lsELLLkN.png)
Ho giocato con il problema di moving all 0's to end
di Array in diverse interviste in varie combinazioni. A volte chiedo di spostare tutti gli 0 davanti all'array, ordinando un array senza alcuna struttura di dati e così via.
In questo tutorial, esamineremo un semplice esempio di spostamento di tutti gli 0 per terminare la conservazione di un ordine di un array. Ci sono due approcci.
Approccio-1)
Logica di partizionamento QuickSort. Cos'è il punto pivot?
-
Pivot point
è un elemento chiave nell'algoritmo di ordinamento rapido. Esegue e partiziona la raccolta attorno al punto pivot. - Dispone gli elementi Array più grandi del pivot prima di esso e gli elementi più grandi del pivot dopo di esso.
- Continua nel ciclo per ordinare una matrice
La logica è molto semplice:
- Iterare attraverso un array.
- Se array[i] non è uguale a 0, scambialo con l'indice corrente.
- Se array[i] == 0, salta semplicemente il ciclo
- Nel nostro caso
0 is a Pivot point
. - Ogni volta che troviamo 0, il contatore pivot verrà incrementato e l'elemento verrà spostato prima del punto pivot.
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 ) ) ; } |
Approccio-2)
- Crea un nuovo array con le stesse dimensioni
- Scorri un array e salta l'aggiunta di 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 ) ) ; } |
Ecco un programma completo:
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 + " " ) ; } } |
Uscita console 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 |
Fammi sapere se conosci un modo migliore per risolvere questo problema. Mi piacerebbe sentire i tuoi pensieri.
