合并排序数组?
Merge sorted arrays?
如何合并和打印两个排序的数组而不重复?我尝试过但无法找到任何有效的解决方案。
public static void mergeSortedArrays(int a[], int b[]) {
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
answer[k++] = a[i] < b[j] ? a[i++] : b[j++];
while (i < a.length)
answer[k++] = a[i++];
while (j < b.length)
answer[k++] = b[j++];
System.out.print(answer);
}
根据您的代码,输出中会有重复项。要删除重复项,您需要在第一个 while:
中将条件修改为此
while (i < a.length && j < b.length)
answer[k++] = a[i] <= b[j] ? a[i++] : b[j++];
这将跳过其他数组中的一个副本。
还需要检查输出数组是否已经包含与前一个索引相同的元素并跳过它。
while (i < a.length && j < b.length) {
if(answer[k] == a[i])
i++;
else if(answer[k] == b[j])
j++;
else
answer[k++] = a[i] <= b[j] ? a[i++] : b[j++];
}
其他时候也一样。
你说两个数组都排序了,我假设合并后的数组也需要排序。
这是代码。解释如下。
(注意:由于 OP 编辑了代码。)
import java.util.Arrays;
public class Test {
private static int[] mergeSortedArrays(int[] a, int[] b) {
int[] temp = new int[a.length + b.length];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length) {
if (j < b.length) {
if (a[i] < b[j]) {
temp[k++] = a[i++];
}
else if (a[i] == b[j]) {
temp[k++] = a[i++];
j++;
}
else {
temp[k++] = b[j++];
}
}
else {
temp[k++] = a[i++];
}
}
if (j < b.length) {
for (; j < b.length; j++) {
temp[k++] = b[j];
}
}
int[] answer = new int[k];
System.arraycopy(temp, 0, answer, 0, k);
/* Added */
System.out.print(answer[0]);
for (i = 1; i < k; i++) {
System.out.print(", " + answer[i]);
}
System.out.println();
/**/
return answer;
}
public static void main(String[] args) {
int[] a = {-19, -5, -4, 3, 7, 8, 11, 21};
int[] b = {-7, -5, -4, 0, 3, 6, 11, 13, 20};
System.out.println(Arrays.toString(mergeSortedArrays(a, b)));
}
}
我们将第一个数组中的元素与第二个数组中的元素进行比较。将两者中较小的复制到结果数组。然后我们增加其元素被复制到结果数组的数组的索引。如果元素相等,我们[任意]从第一个数组复制元素并增加两个数组的索引。这可以防止结果数组包含重复项。
如果数组的元素数量不相等,那么当我们用完一个数组时,我们只需将较长数组中的所有剩余元素复制到结果数组。
因此结果数组包含两个数组中的所有元素,没有重复,已排序。
这是我 运行 上述代码时的输出。
-19, -7, -5, -4, 0, 3, 6, 7, 8, 11, 13, 20, 21
[-19, -7, -5, -4, 0, 3, 6, 7, 8, 11, 13, 20, 21]
结果是排序后的不同元素作为 int 数组。
int[] c = new int[a.length + b.length];
int maxLength = a.length > b.length ? a.length : b.length;
for(int i = 0, j = 0; i < maxLength; i++) {
if(i < a.length) {
c[j] = a[i];
j++;
}
if(i < b.length) {
c[j] = b[i];
j++;
}
}
int[] result = Arrays.stream(c).sorted().distinct().toArray();
如何合并和打印两个排序的数组而不重复?我尝试过但无法找到任何有效的解决方案。
public static void mergeSortedArrays(int a[], int b[]) {
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
answer[k++] = a[i] < b[j] ? a[i++] : b[j++];
while (i < a.length)
answer[k++] = a[i++];
while (j < b.length)
answer[k++] = b[j++];
System.out.print(answer);
}
根据您的代码,输出中会有重复项。要删除重复项,您需要在第一个 while:
中将条件修改为此while (i < a.length && j < b.length)
answer[k++] = a[i] <= b[j] ? a[i++] : b[j++];
这将跳过其他数组中的一个副本。
还需要检查输出数组是否已经包含与前一个索引相同的元素并跳过它。
while (i < a.length && j < b.length) {
if(answer[k] == a[i])
i++;
else if(answer[k] == b[j])
j++;
else
answer[k++] = a[i] <= b[j] ? a[i++] : b[j++];
}
其他时候也一样。
你说两个数组都排序了,我假设合并后的数组也需要排序。
这是代码。解释如下。
(注意:由于 OP
import java.util.Arrays;
public class Test {
private static int[] mergeSortedArrays(int[] a, int[] b) {
int[] temp = new int[a.length + b.length];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length) {
if (j < b.length) {
if (a[i] < b[j]) {
temp[k++] = a[i++];
}
else if (a[i] == b[j]) {
temp[k++] = a[i++];
j++;
}
else {
temp[k++] = b[j++];
}
}
else {
temp[k++] = a[i++];
}
}
if (j < b.length) {
for (; j < b.length; j++) {
temp[k++] = b[j];
}
}
int[] answer = new int[k];
System.arraycopy(temp, 0, answer, 0, k);
/* Added */
System.out.print(answer[0]);
for (i = 1; i < k; i++) {
System.out.print(", " + answer[i]);
}
System.out.println();
/**/
return answer;
}
public static void main(String[] args) {
int[] a = {-19, -5, -4, 3, 7, 8, 11, 21};
int[] b = {-7, -5, -4, 0, 3, 6, 11, 13, 20};
System.out.println(Arrays.toString(mergeSortedArrays(a, b)));
}
}
我们将第一个数组中的元素与第二个数组中的元素进行比较。将两者中较小的复制到结果数组。然后我们增加其元素被复制到结果数组的数组的索引。如果元素相等,我们[任意]从第一个数组复制元素并增加两个数组的索引。这可以防止结果数组包含重复项。
如果数组的元素数量不相等,那么当我们用完一个数组时,我们只需将较长数组中的所有剩余元素复制到结果数组。
因此结果数组包含两个数组中的所有元素,没有重复,已排序。
这是我 运行 上述代码时的输出。
-19, -7, -5, -4, 0, 3, 6, 7, 8, 11, 13, 20, 21
[-19, -7, -5, -4, 0, 3, 6, 7, 8, 11, 13, 20, 21]
结果是排序后的不同元素作为 int 数组。
int[] c = new int[a.length + b.length];
int maxLength = a.length > b.length ? a.length : b.length;
for(int i = 0, j = 0; i < maxLength; i++) {
if(i < a.length) {
c[j] = a[i];
j++;
}
if(i < b.length) {
c[j] = b[i];
j++;
}
}
int[] result = Arrays.stream(c).sorted().distinct().toArray();