質問編集履歴
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -35,4 +35,43 @@
|
|
35
35
|
for(int i=0; i<C.length; i++){
|
36
36
|
System.out.printf("%d ", C[i]);
|
37
37
|
}
|
38
|
+
```
|
39
|
+
###解決後のプログラム
|
40
|
+
```
|
41
|
+
public static int[] intersection(int[] A, int[] B) {
|
42
|
+
HashSet<Integer> uniq = new HashSet<Integer>();
|
43
|
+
int d = 0;
|
44
|
+
int e = 0;
|
45
|
+
while (d < A.length && e < B.length) {
|
46
|
+
int v = A[d];
|
47
|
+
int w = B[e];
|
48
|
+
if (v < w) {
|
49
|
+
++d;
|
50
|
+
} else if (v > w) {
|
51
|
+
++e;
|
52
|
+
} else {
|
53
|
+
if (uniq.add(v)) {
|
54
|
+
}
|
55
|
+
++d;++e;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
System.out.printf("\n");
|
59
|
+
int[] C=new int[uniq.size()];
|
60
|
+
int x=0;
|
61
|
+
for(Integer n:uniq){
|
62
|
+
C[x++]=n;
|
63
|
+
}
|
64
|
+
return C;
|
65
|
+
}
|
66
|
+
|
67
|
+
public static void main(String args[]) {
|
68
|
+
int[]A= new int[]{2,1,4,9,5};
|
69
|
+
Arrays.sort(A);
|
70
|
+
int[]B= new int[]{2,1,5,3};
|
71
|
+
Arrays.sort(B);
|
72
|
+
int[] C = intersection(A, B);
|
73
|
+
for(int i=0; i<C.length; i++){
|
74
|
+
System.out.printf("%d ", C[i]);
|
75
|
+
}
|
76
|
+
System.out.println();
|
38
77
|
```
|