33 lines
710 B
Java
33 lines
710 B
Java
/*
|
|
* @lc app=leetcode id=350 lang=java
|
|
*
|
|
* [350] Intersection of Two Arrays II
|
|
*/
|
|
|
|
// @lc code=start
|
|
|
|
import java.util.List;
|
|
|
|
class Solution {
|
|
public int[] intersect(int[] nums1, int[] nums2) {
|
|
Arrays.sort(nums1);
|
|
Arrays.sort(nums2);
|
|
int i = 0, j = 0;
|
|
List<Integer> ret = new ArrayList<>();
|
|
while(i<nums1.length && j < nums2.length) {
|
|
if(nums1[i] == nums2[j]) {
|
|
ret.add(nums1[i]);
|
|
i++;
|
|
j++;
|
|
} else if(nums1[i] < nums2[j]) {
|
|
i++;
|
|
} else {
|
|
j++;
|
|
}
|
|
}
|
|
return ret.stream().mapToInt(f->f).toArray();
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|