質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -73,3 +73,81 @@
|
|
73
73
|
}
|
74
74
|
|
75
75
|
```
|
76
|
+
|
77
|
+
###解決後のプログラム
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
public static int[] intersection(int[] A, int[] B) {
|
82
|
+
|
83
|
+
HashSet<Integer> uniq = new HashSet<Integer>();
|
84
|
+
|
85
|
+
int d = 0;
|
86
|
+
|
87
|
+
int e = 0;
|
88
|
+
|
89
|
+
while (d < A.length && e < B.length) {
|
90
|
+
|
91
|
+
int v = A[d];
|
92
|
+
|
93
|
+
int w = B[e];
|
94
|
+
|
95
|
+
if (v < w) {
|
96
|
+
|
97
|
+
++d;
|
98
|
+
|
99
|
+
} else if (v > w) {
|
100
|
+
|
101
|
+
++e;
|
102
|
+
|
103
|
+
} else {
|
104
|
+
|
105
|
+
if (uniq.add(v)) {
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
++d;++e;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
System.out.printf("\n");
|
116
|
+
|
117
|
+
int[] C=new int[uniq.size()];
|
118
|
+
|
119
|
+
int x=0;
|
120
|
+
|
121
|
+
for(Integer n:uniq){
|
122
|
+
|
123
|
+
C[x++]=n;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
return C;
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
public static void main(String args[]) {
|
134
|
+
|
135
|
+
int[]A= new int[]{2,1,4,9,5};
|
136
|
+
|
137
|
+
Arrays.sort(A);
|
138
|
+
|
139
|
+
int[]B= new int[]{2,1,5,3};
|
140
|
+
|
141
|
+
Arrays.sort(B);
|
142
|
+
|
143
|
+
int[] C = intersection(A, B);
|
144
|
+
|
145
|
+
for(int i=0; i<C.length; i++){
|
146
|
+
|
147
|
+
System.out.printf("%d ", C[i]);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
System.out.println();
|
152
|
+
|
153
|
+
```
|