Java cannot work?why???


  • 0
    T

    even it seems quite right for me ... cannot find anywhere wrong...
    Input:
    [2,1]
    [1,2]
    Output:
    [1,1]
    Expected:
    [1,2]

    public int[] intersection(int[] nums1, int[] nums2) {

        HashSet<Integer> set= new HashSet<>();
        
        for(int i=0;i<nums1.length;i++){
            for(int j=0;j<nums2.length;j++){
                if(nums1[i]==nums2[j]){
                    if(!set.contains(nums2[j])){
                        set.add(nums2[j]);
                    }
                 }
           }
        }
       int[] result=new int[set.size()];
        for(int i=0;i<set.size();i++){
            result[i]=set.iterator().next();
        }
        return result;
    }

Log in to reply
 

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