En Java Comment déplacer tous les 0 à la fin d'un tableau en préservant l'ordre d'un tableau? [2 façons]
Publié: 2020-12-31![En Java, comment déplacer tous les 0 à la fin d'un tableau en préservant l'ordre d'un tableau ? [2 façons]](/uploads/article/578/Jg7tpgI3lsELLLkN.png)
J'ai joué avec un problème de moving all 0's to end
des tableaux dans différentes interviews dans diverses combinaisons. Parfois, je demande de déplacer tous les 0 vers l'avant du tableau, en triant un tableau sans aucune structure de données, etc.
Dans ce didacticiel, nous allons passer en revue un exemple simple de déplacement de tous les 0 pour terminer la préservation d'un ordre d'un tableau. Il existe deux approches.
Approche-1)
Logique de partitionnement QuickSort. Qu'est-ce qu'un point pivot ?
-
Pivot point
est un élément clé de l'algorithme de tri rapide. Il effectue et partitionne la collection autour du point pivot. - Il organise un Array des éléments plus grands que le pivot avant lui, et des éléments plus grands que le pivot après lui.
- Continuez dans la boucle pour trier un tableau
La logique est très simple :
- Itérer dans un tableau.
- Si array[i] n'est pas égal à 0, échangez-le avec l'index actuel.
- Si array[i] == 0, sautez simplement la boucle
- Dans notre cas,
0 is a Pivot point
. - Chaque fois que nous trouvons 0, le compteur de pivot sera incrémenté et l'élément sera déplacé avant le point de 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 ) ) ; } |
Approche-2)
- Créer un nouveau tableau de même taille
- Itérer dans un tableau et ignorer l'ajout de 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 ) ) ; } |
Voici un programme complet :
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 + " " ) ; } } |
Sortie de la 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 |
Faites-moi savoir si vous connaissez une meilleure façon de résoudre ce problème. Je serais ravi d'entendre vos pensées.
