1. 题目
读题
考查点
2. 解法
思路
代码逻辑
具体实现
public class HJ080 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) {
arr1[i] = sc.nextInt();
}
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) {
arr2[i] = sc.nextInt();
}
System.out.println(mergeArray(arr1, arr2));
}
public static String mergeArray(int[] arr1, int[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
StringBuffer sb = new StringBuffer();
int i = 0, j = 0;
int last = Integer.MIN_VALUE;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] <= arr2[j]) {
if (arr1[i] == last) {
i++;
continue;
}
sb.append(arr1[i]);
last = arr1[i];
i++;
} else {
if (arr2[j] == last) {
j++;
continue;
}
sb.append(arr2[j]);
last = arr2[j];
j++;
}
}
while (i < arr1.length) {
if (last == arr1[i]) {
i++;
continue;
}
sb.append(arr1[i]);
last = arr1[i];
i++;
}
while (j < arr2.length) {
if (last == arr2[j]) {
j++;
continue;
}
sb.append(arr2[j]);
last = arr2[j];
j++;
}
return sb.toString();
}
}