11.Leetcode:350--Intersection of Two Arrays II

题目:

使用Map:

public class IntersectionDupSolution {
    public int[] intersect(int[] nums1, int[] nums2) {

        //元素,次数
        TreeMap<Integer,Integer> map = new TreeMap<>();
        for (int num:nums1) {
            if(!map.containsKey(num)){
                map.put(num,1);
            }else{
                map.put(num,map.get(num)+1);
            }
        }

        ArrayList<Integer> list = new ArrayList<>();
        for(int num:nums2){
            if(map.containsKey(num)){
                list.add(num);
                map.put(num,map.get(num)-1);
                if(map.get(num)==0){
                    map.remove(num);
                }
            }
        }

        int[] res = new int[list.size()];
        for (int i=0;i<list.size();i++){
            res[i]=list.get(i);
        }
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/cl723401/article/details/88812006