2017-01-22 26 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