|
1 | 1 | classSolution {
|
2 | 2 | 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 | + } |
11 | 24 | }
|
12 | 25 | }
|
13 |
| -returnans; |
14 |
| - } |
15 |
| - |
16 |
| -privatevoidupdateCounter(int[]counter,int[]arr) { |
17 |
| -for (intnum :arr) { |
18 |
| -counter[num]++; |
19 |
| - } |
| 26 | +returnresult; |
20 | 27 | }
|
21 | 28 | }
|