JAVA Solution 7ms,use HashSet


  • 0
    S
    public class Solution {
    
        public int[] intersection(int[] nums1, int[] nums2) {
    
        Set<Integer> set=new HashSet<>();
    
        Set<Integer> result=new HashSet<>();
    
        if(nums1==null||nums2==null)
          return null;
    
        for(int n:nums1){
            set.add(n);
        }
        for(int m:nums2){
            if(set.contains(m)){
                result.add(m);
            }
        }
        int size=result.size();
        int[] res=new int[result.size()];
        for(int k:result){
            res[--size]=k;
        }
        return res;
        
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.