给定两个数组,编写一个函数来计算它们的交集。

huangapple go评论70阅读模式
英文:

Given two arrays, write a function to compute their intersection

问题

    class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            HashSet<Integer> set1 = new HashSet<Integer>();
            for (Integer n : nums1)
                set1.add(n);
            HashSet<Integer> set2 = new HashSet<Integer>();
            for (Integer n : nums2)
                set2.add(n);

            set1.retainAll(set2);
            int[] output = new int[set1.size()];
            int idx = 0;
            for (int s : set1)
                output[idx++] = s;
            return output;
        }
    }

output

    Input:
    [1,2,2,1]
    [2,2]
    Output:
    [2]
    Expected:
    [2,2]
英文:
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        HashSet&lt;Integer&gt; set1= new HashSet&lt;Integer&gt;();
        for (Integer n : nums1)
            set1.add(n);
        HashSet&lt;Integer&gt; set2 = new HashSet&lt;Integer&gt;();
        for (Integer n : nums2)
            set2.add(n);
        
        set1.retainAll(set2);
        int[] output = new int[set1.size()];
        int idx=0;
        for(int s :set1)
            output[idx++]=s;
        return output;            
    }
}

output

Input:
[1,2,2,1]
[2,2]
Output:
[2]
Expected:
[2,2]

答案1

得分: 0

集合不允许重复,因此如果您希望输出包含重复项,则它们不适用。

您可以使用Map<Integer, Integer>来计算第一个数组中每个元素的出现次数,然后与第二个数组进行比较:

public int[] intersect(int[] nums1, int[] nums2) {
    Map<Integer, Integer> map1 = new HashMap<>();
    for (int n : nums1) {
        if (map1.containsKey(n))
            map1.put(n, map1.get(n) + 1);
        else
            map1.put(n, 1);
    }
    List<Integer> inter = new ArrayList<>();
    for (int n : nums2) {
        Integer count = map1.get(n);
        if (count != null && count > 0) {
            map1.put(n, count - 1);
            inter.add(n);
        }
    }

    int[] output = new int[inter.size()];
    for (int i = 0; i < inter.size(); i++)
        output[i] = inter.get(i);
    return output;
}
英文:

Sets don't allow duplicates, so they are not suitable if you expect the output to contain duplicates.

You can use a Map&lt;Integer,Integer&gt; to count the occurrences of each element of the first array, and then check them against the second array:

public int[] intersect(int[] nums1, int[] nums2) {
    Map&lt;Integer,Integer&gt; map1 = new HashMap&lt;&gt;();
    for (int n : nums1) {
        if (map1.containsKey(n))
            map1.put(n,map1.get(n)+1);
        else
            map1.put(n,1);
    }
    List&lt;Integer&gt; inter = new ArrayList&lt;&gt;();
    for (int n : nums2) {
        Integer count = map1.get(n);
        if (count != null &amp;&amp; count &gt; 0) {
            map1.put(n,count-1);
            inter.add(n);
        }
    }
    
    int[] output = new int[inter.size()];
    for(int i = 0; i &lt; inter.size(); i++)
        output[i]=inter.get(i);
    return output;
    
}

huangapple
  • 本文由 发表于 2020年8月5日 16:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定