Java'da Bir Dizinin Sırasını Koruyarak Tüm 0'ları Dizinin sonuna nasıl taşırım? [2 yol]
Yayınlanan: 2020-12-31![Java'da Bir Dizinin Sırasını Koruma Sırasını Koruyarak Tüm 0'ları Dizinin sonuna nasıl taşırım? [2 yol]](/uploads/article/578/Jg7tpgI3lsELLLkN.png)
Çeşitli kombinasyonlarda farklı görüşmelerde moving all 0's to end
sorunuyla oynuyorum. Bazen tüm 0'ları dizinin önüne taşımayı, bir diziyi herhangi bir veri yapısı olmadan sıralamayı vb.
Bu eğitimde, bir Dizinin sırasını korumayı sona erdirmek için tüm 0'ları taşımanın basit örneğini inceleyeceğiz. İki yaklaşım var.
Yaklaşım-1)
QuickSort Bölümleme mantığı. Pivot Noktası nedir?
-
Pivot point
, Hızlı Sıralama Algoritmasının önemli bir öğesidir. Toplama işlemini pivot noktası etrafında gerçekleştirir ve bölümlendirir. - Önündeki pivottan daha büyük bir Dizi öğelerini ve pivottan daha büyük öğeleri ondan sonra düzenler.
- Bir diziyi sıralamak için döngü boyunca devam edin
Mantık çok basit:
- Bir Dizi aracılığıyla yineleyin.
- [i] dizisi 0'a eşit değilse, onu geçerli dizinle değiştirin.
- dizi[i] == 0 ise, basitçe döngüyü atlayın
- Bizim durumumuzda
0 is a Pivot point
. - 0'ı her bulduğumuzda, karşı eksen artırılacak ve eleman pivot noktasından önce hareket edecektir.
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 ) ) ; } |
Yaklaşım-2)
- Aynı boyutta yeni bir dizi oluşturun
- Bir Dizi boyunca yineleyin ve 0 eklemeyi atlayın
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 ) ) ; } |
İşte Tam Bir Program:
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 Konsol Çıktısı:
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 |
Bu sorunu çözmenin daha iyi bir yolunu biliyorsanız bana bildirin. Düşüncelerinizi duymayı çok isterim.
