【发布时间】:2023-03-10 21:57:02
【问题描述】:
当我得到像 [1,1,2,2,3,3,4] 这样的数组时,我需要打印 [1,2,3,4]。
其他例子:
输入=[1,1,1,2] 输出应该是=[1,2]
输入=[1,1,1,1] 输出应该是=[1]
int count=1;
//This for counts only the different numbers of the array input.
for(int i=0; i<array.length-1;i++){
if(array[i+1]!=array[i]){
count++;
}
}
//New array only for the needed numbers.
Integer [] res = new Integer[count];
res[0] = array[0];
for(int i = 1;i<count;i++){
if(array[i]!=array[i+1]){
res[i]=array[i+1];
}
}
输入 [1,1,2,2,3,3,4] 我得到 [1, 2, null, 3]。
应该是 [1,2,3,4]。
【问题讨论】:
-
您的错误在
res[i]=array[i+1];行中。你能弄清楚这条线应该是什么,为什么?
标签: java arrays integer int concatenation