java ,4ms ,beat94%,BitSet


  • 0
    T

    public class Solution {

    public int[] intersection(int[] nums1, int[] nums2) {
        if(nums1==null||nums2==null){
            return null;
        }
       
        BitSet set=new BitSet();
        BitSet interSet=new BitSet();
        for(int i:nums1){
            set.set(i);
        }
        int m=0;
        for(int k:nums2){
            if(set.get(k)&&!interSet.get(k)){
                interSet.set(k);
                m++;
            }
        }
        int[] nums3= new int[m];
        int n=0;
        for(int j=1;j<=interSet.size();j++){
            if(interSet.get(j)){
              nums3[n]=j;
              n++;
            }
        }
        
        return nums3;
    
    }
    

    }


Log in to reply
 

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