Leetcode 350. 两个数组的交集 II
呵呵,java的骚操作太多了...
public int[] intersect(int[] nums1, int[] nums2) {
/**
* 将Nums1中的元素放入list1中
*/
List<Integer> list1 = Arrays.stream(nums1).boxed().collect(Collectors.toList());
List<Integer> list2 = Arrays.stream(nums2).boxed().filter(num -> {
if(list1.contains(num)){
list1.remove(num);
return true;
}
return false;
}).collect(Collectors.toList());
int[] ans = new int[list2.size()];
for (int i = 0; i < list2.size(); i++) {
ans[i] = list2.get(i);
}
return ans;
}