2017-01-22 28 views
1

我是編程的初學者,我需要從int []數組中打印最長的數字序列。 例如,如果我們有:如何從int []數組(Java)中打印最長的數字序列

int[] numbers = {1, 3, 3, 5, 5, 5, 5, 5, 5, 6, 0, 12, 2, 2, 2, 12, 0}; 

的結果應該是:

String result = "5, 5, 5, 5, 5, 5"; 

我寫了一些不好的代碼,不工作,但也許它會給你一些想法。

public String findLargestSequence(int[] numbers) { 
     int bestStart = 0; 
     int curStart = 0; 
     int bestLength = 1; 
     int curLength = 1; 
     for (int i = 1; i < numbers.length; i++) { 
      if (numbers[i] > numbers[i - 1]) { 
       curLength++; 
       if (curLength > bestLength) { 
        bestStart = curStart; 
        bestLength = curLength; 
       } 
      } else { 
       curStart = i; 
       curLength = 1; 
      } 
     } 
     List<String> identical = new ArrayList<>(); 
     for (int i = 0; i < bestLength; i++) { 
      identical.add(String.valueOf(numbers[bestStart + i])); 
     } 
     return Joiner.on(", ").join(identical); 
    } 

更新。 感謝@phatfingers,我發現問題: (numbers[i] > numbers[i - 1])應該是(numbers[i] == numbers[i - 1])。 但還是有另一個問題。 如果我們有這樣的事:

int[] numbers = {1, 2, 3, 3, 4, 4};

它的結果是:

"3, 3" 

我認爲在這種情況下,我們可以:

1)說,我們不要」 t具有任何一個最長的序列OR

2)顯示所有序列,如:

String result = "Founded sequences: " + sequence1 + ", " + sequence2; 

3)對上面的代碼不做任何事情。

你會怎麼做?

+1

對於初學者來說,你的'(數字檢查[i]數字[i - 1])'應該是'(數字[i] ==數字[i - 1])''。 – phatfingers

+0

我沒有代碼,但這是我的理論。首先,將數組從最低到最高排序。然後,檢查每個數字的連續出現次數。然後你可以找出那個序列。 –

+0

除了@phatfingers指出的外,你的代碼看起來很漂亮,正確。黑馬,它是否與建議的更正一起工作?如果不是,以什麼方式不? –

回答

0

本屆展會最大的發生,還可以指望它,並打印出來

public static int consecutive(int[] array) { 
     if (array.length <= 1) { 
      return array.length; 
     }  
     int maxRun = 0; 
     for (int i = 1; i < array.length; i++) { 
      int thisRun = 1; 
      while (i < array.length && array[i - 1] + 1 == array[i]) { 
       thisRun++; 
       i++; 
      } 
      if (maxRun < thisRun) { // checking geater occurance 
       maxRun = thisRun; 
      } 
     } 
     return maxRun; 
    } 
+0

雖然代碼看起來正確,但我認爲我們通過指出如何修補他或她自己的嘗試來幫助提問者。 –

0

你必須處理4例,算法可以分爲兩個部分劃分:

設置狀態目前意甲:

  • 增量,如果它的增長目前意甲
  • 重新初始化當前意甲時,它改變

設置最大意甲的狀態:

  • 增量,如果它生長
  • 重新初始化最大意甲時,它改變
最大意甲

在實際的代碼中,這些條件在循環中不受重視。

我評論兩個邏輯錯誤來說明這個問題:

if (numbers[i] > numbers[i - 1]) { 
    // error : you don't increment the current serie when it grows 
    // because the condition if true only if the the current value is 
    // superior to the previous one 
    curLength++; 
    if (curLength > bestLength) { 
     bestStart = curStart; 
     bestLength = curLength; 
    } 
    } 
    // error : you don't reinit the current serie only when it changes 
    // because numbers[i] <= numbers[i - 1] is not true if the new number is 
    // superior to the previous one while it is a change 
    else {  
    curStart = i; 
    curLength = 1; 
    } 
} 

這裏所提出的代碼,處理4個條件兩個兩個:

public static String findLargestSequence(int[] numbers) { 

    // variables to maintain the max serie found and the number associated 
    int sizeMaxSerieFound = 0; 
    int numberMaxFound = numbers[0]; 

    // variables to maintain the current serie found and the number 
    // associated 
    int sizeMaxCurrentSerie = 0; 
    int numberCurrentSerie = numbers[0]; 

    boolean isMaxSerieIsTheCurrent = true; 

    for (int i = 0; i < numbers.length; i++) { 
     int currentNumber = numbers[i]; 

     // FIRST STEP : set the state of the current serie 

     // increment the current serie if it grows or for the first 
     // iteration 
     if (currentNumber == numberCurrentSerie) { 
      sizeMaxCurrentSerie++; 
     } 
     // else we reinit to 1 the current serie 
     else { 
      sizeMaxCurrentSerie = 1; 
      numberCurrentSerie = currentNumber; 
      isMaxSerieIsTheCurrent = false; 
     } 

     // SECOND STEP : set the state of the max serie 

     // we increment the current number of the actual max serie 
     if (currentNumber == numberMaxFound && isMaxSerieIsTheCurrent) { 
      sizeMaxSerieFound++; 
     } 

     // we reinit the serie because we have found a new greater serie 
     else if (currentNumber != numberMaxFound && sizeMaxCurrentSerie > sizeMaxSerieFound) { 
      sizeMaxSerieFound = sizeMaxCurrentSerie; 
      numberMaxFound = currentNumber; 
      isMaxSerieIsTheCurrent = true; 
     } 

    } 

    List<String> identical = new ArrayList<>(); 
    for (int i = 0; i < sizeMaxSerieFound; i++) { 
     identical.add(String.valueOf(numberMaxFound)); 
    } 
    return Joiner.on(", ").join(identical); 
} 
+0

askers代碼中的錯誤評論很好。我認爲你自己的代碼是不正確的,儘管它不是100%清楚。示例輸入:'new int [] {2,2,1,2,2,5,5,5}''。按照我的理解預期輸出:'5,5,5'。觀察到的輸出:「2,2,2,2」。 –

+0

@Ole VV事實上,當我增加實際最大系列的當前數量時,它在'if'中錯過了&&條件,因爲只有當前數字相同時,我們才必須增加最大系列:currentNumber == numberMaxFound '和第二個條件,我們也必須在最大意義上。否則,我們增加一個不是系列的系列,因爲它們之間有差距。 – davidxxx