如何将数组与多个数字连接起来

时间:2021-09-23 21:14:53

I need to print [1,2,3,4] when I get array like [1,1,2,2,3,3,4].

当我得到像[1,1,2,2,3,3,4]这样的数组时,我需要打印[1,2,3,4]。

Other examples:

input=[1,1,1,2] output should be=[1,2]

输入= [1,1,1,2]输出应为= [1,2]

input=[1,1,1,1] output should be=[1]

input = [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];
            }
        }

With input [1,1,2,2,3,3,4] I get [1, 2, null, 3].

输入[1,1,2,2,3,3,4]我得到[1,2,null,3]。

Should be [1,2,3,4].

应该是[1,2,3,4]。

1 个解决方案

#1


1  

One problem is that you increment the loop's counter even when array[i]==array[i+1], which results in the output array having null values.

一个问题是即使在array [i] == array [i + 1]时也增加了循环计数器,这导致输出数组具有空值。

Another problem is that you don't iterate over all the elements of the input array in the second loop.

另一个问题是你不会在第二个循环中迭代输入数组的所有元素。

Both problems can be solved if you use two indices, one for the input array (the loop's variable) and another for the current position in the output array :

如果使用两个索引,两个问题都可以解决,一个用于输入数组(循环的变量),另一个用于输出数组中的当前位置:

int count=1;
for(int i=0; i<array.length-1;i++){
    if(array[i+1]!=array[i]){
        count++;
    }
}
Integer [] res = new Integer[count];
res[0] = array[0];
int resIndex = 1;
for(int i = 1; i < array.length - 1; i++){
    if(array[i] != array[i+1]) {
        res[resIndex] = array[i+1];
        resIndex++;
    }
}

EDIT :

As f* suggested, changing the second loop to

正如法比安建议的那样,将第二个循环更改为

for(int i = 1 ; i < array.length - 1 && resIndex < count; i++)

can make it slightly faster if the last unique number of the input array repeats multiple times.

如果输入数组的最后一个唯一编号重复多次,则可以使其稍微快一些。

#1


1  

One problem is that you increment the loop's counter even when array[i]==array[i+1], which results in the output array having null values.

一个问题是即使在array [i] == array [i + 1]时也增加了循环计数器,这导致输出数组具有空值。

Another problem is that you don't iterate over all the elements of the input array in the second loop.

另一个问题是你不会在第二个循环中迭代输入数组的所有元素。

Both problems can be solved if you use two indices, one for the input array (the loop's variable) and another for the current position in the output array :

如果使用两个索引,两个问题都可以解决,一个用于输入数组(循环的变量),另一个用于输出数组中的当前位置:

int count=1;
for(int i=0; i<array.length-1;i++){
    if(array[i+1]!=array[i]){
        count++;
    }
}
Integer [] res = new Integer[count];
res[0] = array[0];
int resIndex = 1;
for(int i = 1; i < array.length - 1; i++){
    if(array[i] != array[i+1]) {
        res[resIndex] = array[i+1];
        resIndex++;
    }
}

EDIT :

As f* suggested, changing the second loop to

正如法比安建议的那样,将第二个循环更改为

for(int i = 1 ; i < array.length - 1 && resIndex < count; i++)

can make it slightly faster if the last unique number of the input array repeats multiple times.

如果输入数组的最后一个唯一编号重复多次,则可以使其稍微快一些。