@Leetcode350
给定两个数组,编写一个函数来计算它们的交集。
示例 :
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
思路1:
两个数组都排序,两个数组分别有a、b指针从头指向尾,如果有相同的就添加到结果集并同时向前移一位,如果a指向到较大则b指针前移,否则a指针前移。
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
int a=0,b=0;
List<Integer> res=new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
while(a<nums1.length&&b<nums2.length){
if(nums1[a]==nums2[b]){
res.