5 ms beats 85%


  • 0
    P

    """
    public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
    int count =0; int [] ans = new int[Math.min(nums1.length, nums2.length)];
    HashSet<Integer> h1 = new HashSet<Integer>();
    HashSet<Integer> h2 = new HashSet<Integer>();
    for(int i =0; i<nums1.length; i++) h1.add(nums1[i]);
    for(int j =0; j<nums2.length; j++) {if(h1.contains(nums2[j])) {
    if(!h2.contains(nums2[j]))
    {h2.add(nums2[j]);ans[count] = nums2[j]; count ++;}}}
    int []fans = new int [count];
    for(int k=0; k<count; k++)
    {fans[k] = ans[k];}

       return fans;
        
        
    }
    

    }"""


Log in to reply
 

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