Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd0ecac

Browse files
authored
Update Intersection of Three Sorted Arrays.java
1 parent399f496 commitcd0ecac

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
classSolution {
22
publicList<Integer>arraysIntersection(int[]arr1,int[]arr2,int[]arr3) {
3-
int[]counter =newint[2001];
4-
updateCounter(counter,arr1);
5-
updateCounter(counter,arr2);
6-
updateCounter(counter,arr3);
7-
List<Integer>ans =newArrayList<>();
8-
for (inti =0;i <2001;i++) {
9-
if (counter[i] ==3) {
10-
ans.add(i);
3+
intidxOne =0;
4+
intidxTwo =0;
5+
intidxThree =0;
6+
List<Integer>result =newArrayList<>();
7+
while (idxOne <arr1.length &&idxTwo <arr2.length &&idxThree <arr3.length) {
8+
if (arr1[idxOne] ==arr2[idxTwo] &&arr2[idxTwo] ==arr3[idxThree]) {
9+
result.add(arr1[idxOne]);
10+
idxOne++;
11+
idxTwo++;
12+
idxThree++;
13+
}else {
14+
intmax =Math.max(arr1[idxOne],Math.max(arr2[idxTwo],arr3[idxThree]));
15+
if (arr1[idxOne] <max) {
16+
idxOne++;
17+
}
18+
if (arr2[idxTwo] <max) {
19+
idxTwo++;
20+
}
21+
if (arr3[idxThree] <max) {
22+
idxThree++;
23+
}
1124
}
1225
}
13-
returnans;
14-
}
15-
16-
privatevoidupdateCounter(int[]counter,int[]arr) {
17-
for (intnum :arr) {
18-
counter[num]++;
19-
}
26+
returnresult;
2027
}
2128
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp