題目描述
將兩個整型數組按照升序合併,而且過濾掉重複數組元素
輸入描述
輸入說明,按下列順序輸入:
1 輸入第一個數組的個數
2 輸入第一個數組的數值
3 輸入第二個數組的個數
4 輸入第二個數組的數值
輸出描述
輸出合併以後的數組
輸入例子
3
1 2 5
4
-1 0 3 2
輸出例子
-101235
算法實現
import java.util.Arrays;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Declaration: All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
while (scanner.hasNext()) {
// 方法一
// int m = scanner.nextInt();
// int[] a = new int[m];
// for (int i = 0; i < m; i++) {
// a[i] = scanner.nextInt();
// }
//
// int n = scanner.nextInt();
// int[] b = new int[n];
// for (int i = 0; i < n; i++) {
// b[i] = scanner.nextInt();
// }
//
// System.out.println(mergeArray(a, b));
// 方法二
int m = scanner.nextInt();
SortedSet<Integer> set = new TreeSet<>();
for (int i = 0; i < m; i++) {
set.add(scanner.nextInt());
}
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
set.add(scanner.nextInt());
}
for (Integer i : set) {
System.out.print(i);
}
}
scanner.close();
}
private static String mergeArray(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
Arrays.sort(a);
Arrays.sort(b);
int ap = 0;
int bp = 0;
int cp = 0;
while (ap < a.length && bp < b.length) {
if (a[ap] < b[bp]) {
c[cp] = a[ap];
ap++;
} else if (a[ap] > b[bp]) {
c[cp] = b[bp];
bp++;
} else {
c[cp] = a[ap];
ap++;
bp++;
}
cp++;
}
for (int i = ap; i < a.length; i++) {
if (a[i] != c[cp - 1]) {
c[cp] = a[i];
cp++;
}
}
for (int i = bp; i < b.length; i++) {
if (b[i] != c[cp - 1]) {
c[cp] = b[i];
cp++;
}
}
// System.out.println(Arrays.toString(c));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < cp; i++) {
builder.append(c[i]);
}
return builder.toString();
}
}