我是编程的初学者,我需要从int []数组中打印最长的数字序列。 例如,如果我们有:
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)说,我们没有任何一个最长的序列OR
2)显示所有序列,如:
String result = "Founded sequences: " + sequence1 + ", " + sequence2;
3)对上面的代码不做任何事。
你会做什么?
答案 0 :(得分: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;
}
答案 1 :(得分: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);
}