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;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章