如何打印int[]数组中最长的数字序列(Java)
我是编程新手,需要打印int[]数组中最长的数字序列。 例如,如果我们有:如何打印int[]数组中最长的数字序列(Java),java,arrays,sequence,Java,Arrays,Sequence,我是编程新手,需要打印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 =
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);
}
我认为在这种情况下,我们可以:
1) 比如说,我们没有任何一个最长的序列或者
2) 显示所有序列,如:
String result = "Founded sequences: " + sequence1 + ", " + sequence2;
3) 不要使用上面的代码
您会怎么做?这会显示最大发生次数,您还可以计数并打印它们
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;
}
公共静态int连续(int[]数组){
如果(array.length显示最大出现次数,您还可以对其进行计数并打印
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;
}
公共静态int连续(int[]数组){
如果(array.length您必须在算法中处理4种情况,可以将其分为两部分:
设置当前系列的状态:
- 如果当前序列增加,则增加该序列
- 更改时重新输入当前的系列
设置最大系列的状态:
- 如果增加,则增加最大系列
- 当它改变时,重新调整max系列
在实际代码中,循环中不考虑这些条件
我评论了两个逻辑错误来说明问题:
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;
}
}
if(数字[i]>数字[i-1]){
//错误:当当前序列增长时,您不增加它
//因为只有当前值为
//优于上一个
curLength++;
如果(卷曲长度>最佳长度){
bestStart=curStart;
最佳长度=卷曲长度;
}
}
//错误:只有当当前系列发生变化时,您才重新添加它
//因为数字[i]SizeMaxSerieve(已找到){
sizeMaxSerieFound=sizeMaxCurrentSerie;
numberMaxFound=当前编号;
IsMaxSeriesTheCurrent=true;
}
}
列表相同=新的ArrayList();
for(int i=0;i
您必须在算法中处理4种情况,这些情况可以分为两部分:
设置当前系列的状态:
- 如果当前序列增加,则增加该序列
- 更改时重新输入当前的系列
设置最大系列的状态:
- 如果增加,则增加最大系列
- 当它改变时,重新调整max系列
在实际代码中,循环中不考虑这些条件
我评论了两个逻辑错误来说明问题:
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;
}
}
if(数字[i]>数字[i-1]){
//错误:当当前序列增长时,您不增加它
//因为只有当前值为
//优于上一个
curLength++;
如果(卷曲长度>最佳长度){
bestStart=curStart;
最佳长度=卷曲长度;
}
}
//错误:只有当当前系列发生变化时,您才重新添加它
//因为数字[i]SizeMaxSerieve(已找到){
sizeMaxSerieFound=sizeMaxCurrentSerie;
numberMaxFound=当前编号;
IsMaxSeriesTheCurrent=true;
}
}
列表相同=新的ArrayList();
for(int i=0;i
首先,您对(数字[i]>数字[i-1])
的检查应该是(数字[i]==数字[i-1])
。我没有代码,但我的理论是这样的。首先,将数组从最低到最高排序。然后,检查每个数字连续出现的次数。除了@phatfingers指出的以外,你的代码在我看来是美丽而正确的。blackHorse,它与建议的更正一起工作吗?如果不是,以什么方式不是?@MarkYisri,我想数字在输入数组中必须是连续的。如果这是正确的,那么对数组进行排序不仅是多余的,而且可能会产生太长的结果。对于初学者,您对(数字[i]>数字[i-1])
的检查应该是(数字[i]==数字[i-1])
。我没有代码,但这是我的理论。首先,将数组从最低到最高排序。然后,检查每个数字连续出现的次数。除了@phatfingers指出的以外,你的代码在我看来是美丽而正确的。blackHorse,它与建议的更正一起工作吗?如果不是,以什么方式不是?@MarkYisri,我想数字在输入数组中必须是连续的。如果这是正确的,那么对数组进行排序不仅是多余的,而且可能会产生太长的结果。虽然代码看起来是正确的,但我认为我们为提问者指出了如何改进自己的尝试的方向,从而帮助他们做得更好。虽然代码看起来是正确的,我认为,我们为提问者指出了如何改进自己的尝试的方向,这对提问者有更好的帮助。我认为您自己的代码也不正确,尽管它不是100%清楚。输入示例:newint[]{2,2,1,2,2,5,5}
。据我所知,预期输出:5,5,5
。观察到的输出:2,2,2,2
@Ole V.V实际上,当我增加实际最大系列的当前数量时,如果
的话,它就错过了一个&
条件,因为只有当当前数量相同时,我们才必须增加最大系列:currentNumber==numberMaxFound
和第二个条件,并且我们也必须在最大系列中。否则,我们会增加一个系列,而这个系列不是一个系列,因为它之间有间隙。askers代码中关于错误的注释是好的。我认为你的o