[英]How to print the longest sequence of numbers from int[] array (Java)
I'm a beginner in programming and I need to print the longest sequence of numbers from int[] array. 我是编程的初学者,我需要从int []数组中打印最长的数字序列。 For example, if we have: 例如,如果我们有:
int[] numbers = {1, 3, 3, 5, 5, 5, 5, 5, 5, 6, 0, 12, 2, 2, 2, 12, 0};
Result should be: 结果应该是:
String result = "5, 5, 5, 5, 5, 5";
I wrote some bad code that don't work, but maybe it will give you some ideas. 我写了一些不起作用的坏代码,但也许会给你一些想法。
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);
}
Update. 更新。 Thanks to @phatfingers, I've found problem: (numbers[i] > numbers[i - 1])
should be (numbers[i] == numbers[i - 1])
. 感谢@phatfingers,我发现问题: (numbers[i] > numbers[i - 1])
应该是(numbers[i] == numbers[i - 1])
。 But still there is one another problem. 但还是有另一个问题。 If we have something like: 如果我们有类似的东西:
int[] numbers = {1, 2, 3, 3, 4, 4};
The result of it is: 结果是:
"3, 3"
I think in this case, we could: 我想在这种情况下,我们可以:
1) say, that we don't have any one longest sequence OR 1)说,我们没有任何一个最长的序列OR
2) show all that sequences, like: 2)显示所有序列,如:
String result = "Founded sequences: " + sequence1 + ", " + sequence2;
3) do nothing with that code above. 3)对上面的代码不做任何处理。
What would you do? 你会怎么做?
this show the max occurrence, you can also count it and print them 这显示最大值,您也可以计算并打印它们
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;
}
You have to handle 4 cases in the algorithm that you can divide in two parts: 您必须处理算法中的4个案例,您可以分为两部分:
Setting the state of the current serie : 设置当前系列的状态:
Setting the state of the max serie : 设置最大系列的状态:
In the actual code these conditions are not respected in the loop. 在实际代码中,循环中不遵守这些条件。
I commented two logic errors to illustrate the problem : 我评论了两个逻辑错误来说明问题:
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;
}
}
Here is the proposed code that handles the 4 conditions two by two: 以下是两个两个处理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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.