2014-10-03 35 views
-2

考慮數字的順序是這樣的:打印沒有連續重複的數字序列?

1 2 2 5 5 5 1 7 3 7 7 7 

輸出應該是

1 2 5 1 7 3 7 

我的代碼的電流輸出爲

1 2 5 1 7 3 

我無法來解決這個問題。任何人都可以告訴我,我應該怎麼做或更改我的代碼?

這裏是我當前的代碼:

public class Q3 { 
    public static void main(String args[]) { 
     int [] input=new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
     int current= input[0]; 
     boolean found=false; 
     for(int i=0; i< input.length; i++) { 
      if(current == input[i] && !found) { 
       found=true; 
      } else if(current!=input[i]) { 
       System.out.print(" " + current); 
       current=input[i]; 
       found=false; 
      } 
     }  
    } 
} 
+0

歡迎SO。你能對這個問題更具體嗎? – arunmoezhi 2014-10-03 05:21:55

+0

打印第一次出現,並忽略其餘的... – MadProgrammer 2014-10-03 05:22:39

+0

@RuchiraGayanRanaweera。這個問題是如何看待由MadProgrammer完成的編輯 – arunmoezhi 2014-10-03 05:25:55

回答

0

這是不正確的使用其他的答案沒有給予信貸

int[] input = new int[]{1, 2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
int current = input[0]; 
boolean found = false; 

for (int i = 0; i < input.length; i++) { 
    if (current == input[i] && !found) { 
     found = true; 
    } else if (current != input[i]) { 
     System.out.print(" " + current); 
     current = input[i]; 
     found = false; 
    } 
} 
System.out.print(" " + current); <--- you forgot this line 

輸出:

1 2 5 1 7 3 7 

我的答案可以在這裏找到 How to efficiently remove duplicates from an array without using Set

+4

問題不在於刪除數組中的重複元素...... – 2014-10-03 05:30:19

+0

這是什麼?你有沒有跑過我的代碼,看看它做了什麼? – 2014-10-03 05:30:57

+2

再次閱讀。輸入:** 1 2 2 5 5 5 1 7 3 7 7 7 **。輸出:1 2 5 ** 1 ** 7 3 ** 7 **,我將* duplicate *元素標記爲有效輸出的一部分。 – 2014-10-03 05:32:48

0

您可以用這種方式嘗試

如:

int[] input = new int[]{1, 2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
int temp =input[0]; 
boolean isFirst=true; 
for (int i = 0; i < input.length; i++) { 
    if(isFirst){ 
     System.out.print(temp); 
     isFirst=false; 
    } 
    if (temp != input[i]) { 
     System.out.print(input[i]); 
    } 
    temp = input[i]; 
} 

輸出地說:

1251737 

邏輯:我現在只有第一次出現連續數字秒。

+0

感謝上帝,沒有理由 – 2014-10-03 05:25:38

+3

這可能會失敗,如果數組充滿'0'... – 2014-10-03 05:26:31

+0

好吧,現在將行更改爲'int [] input = new int [] {0,1, 1,2};' – Krumia 2014-10-03 05:27:09

0
int [] input = new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
int previous = 0; 
boolean started = false; // loop is not started yet 

for (int i = 0; i < input.length; i++) 
{ 
    // if the loop is not started, or the number is not equal to previously printed number... 
    if (!started || input[i] != previous) { 
     System.out.print(input[i] + " "); 
     previous = input[i]; 
    } 
    started = true; 
} 
2

的基本問題EM是,你需要播種current價值的東西,不等於第一個元素,這樣一來,你可以通過數組沒有問題,標誌或其他技巧,例如環...

int[] input = new int[]{1, 2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
int current = -input[0]; 

for (int i = 0; i < input.length; i++) { 
    if (current != input[i]) { 
     current = input[i]; 
     System.out.print(" " + current); 
    } 
} 
System.out.println(""); 

,輸出...

1 2 5 1 7 3 7 

這臺current值爲負的第一個元素(在此-1),所以當我們做我們的第一次比較,-1 != 1然後就可以進行正常的...

+0

運營商偷走了我的答案我該如何舉報? – 2014-10-03 05:35:17

+0

你是什麼意思「偷東西? – MadProgrammer 2014-10-03 05:36:36

+0

去鏈接,看看這是我的回答 – 2014-10-03 05:36:55

1

如果您不想刪除所有重複項,只有最接近的重複項,您可以簡化代碼。

public class Q3 { 
    public static void main(String args[]) { 
     int [] input=new int[]{1 ,2, 2, 5, 5, 5, 1, 7, 3, 7, 7, 7}; 
     int base = input[0]; 
     System.out.print(base); 
     for (int current : input) { 
      if (current != base) { 
       base = current; 
       System.out.print(" " + base); 
      } 
     }  
    } 
} 

輸出:

1 2 5 1 7 3 7