My java solution


  • 0
    C
       import java.util.*;
       public class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
        
        if(nums1.length == 0 || nums2.length == 0)
        return new int[]{};
        
        int length = nums1.length>nums2.length?nums1.length:nums2.length;
    	int [] res =new int[length];
    	
    	Hashtable<Integer,Integer> table = new Hashtable();
    	
    	int j=0;
    	
    	for(int i=0;i<nums1.length;i++)
    	{
    		table.put(nums1[i], i);	
    	}
    	
    	for(int i=0;i<nums2.length;i++)
    	{
    		Integer value = table.get(nums2[i]);
    		
    		if(value != null &&(value != -1))
    		{
    			res[j]=nums2[i];
    			j++;
    		}
    		
    		table.put(nums2[i],-1);
    	}
    	
    	int [] r = new int[j];
    	for(int i=0;i<j;i++)
    	{
    		r[i] = res[i];
    	}
    	return r;
    }
    

    }


Log in to reply
 

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