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

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

Given two arrays, write a function to compute their intersection

问题

  1. class Solution {
  2. public int[] intersect(int[] nums1, int[] nums2) {
  3. HashSet<Integer> set1 = new HashSet<Integer>();
  4. for (Integer n : nums1)
  5. set1.add(n);
  6. HashSet<Integer> set2 = new HashSet<Integer>();
  7. for (Integer n : nums2)
  8. set2.add(n);
  9. set1.retainAll(set2);
  10. int[] output = new int[set1.size()];
  11. int idx = 0;
  12. for (int s : set1)
  13. output[idx++] = s;
  14. return output;
  15. }
  16. }
  17. output
  18. Input:
  19. [1,2,2,1]
  20. [2,2]
  21. Output:
  22. [2]
  23. Expected:
  24. [2,2]
英文:
  1. class Solution {
  2. public int[] intersect(int[] nums1, int[] nums2) {
  3. HashSet&lt;Integer&gt; set1= new HashSet&lt;Integer&gt;();
  4. for (Integer n : nums1)
  5. set1.add(n);
  6. HashSet&lt;Integer&gt; set2 = new HashSet&lt;Integer&gt;();
  7. for (Integer n : nums2)
  8. set2.add(n);
  9. set1.retainAll(set2);
  10. int[] output = new int[set1.size()];
  11. int idx=0;
  12. for(int s :set1)
  13. output[idx++]=s;
  14. return output;
  15. }
  16. }

output

  1. Input:
  2. [1,2,2,1]
  3. [2,2]
  4. Output:
  5. [2]
  6. Expected:
  7. [2,2]

答案1

得分: 0

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

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

  1. public int[] intersect(int[] nums1, int[] nums2) {
  2. Map<Integer, Integer> map1 = new HashMap<>();
  3. for (int n : nums1) {
  4. if (map1.containsKey(n))
  5. map1.put(n, map1.get(n) + 1);
  6. else
  7. map1.put(n, 1);
  8. }
  9. List<Integer> inter = new ArrayList<>();
  10. for (int n : nums2) {
  11. Integer count = map1.get(n);
  12. if (count != null && count > 0) {
  13. map1.put(n, count - 1);
  14. inter.add(n);
  15. }
  16. }
  17. int[] output = new int[inter.size()];
  18. for (int i = 0; i < inter.size(); i++)
  19. output[i] = inter.get(i);
  20. return output;
  21. }
英文:

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:

  1. public int[] intersect(int[] nums1, int[] nums2) {
  2. Map&lt;Integer,Integer&gt; map1 = new HashMap&lt;&gt;();
  3. for (int n : nums1) {
  4. if (map1.containsKey(n))
  5. map1.put(n,map1.get(n)+1);
  6. else
  7. map1.put(n,1);
  8. }
  9. List&lt;Integer&gt; inter = new ArrayList&lt;&gt;();
  10. for (int n : nums2) {
  11. Integer count = map1.get(n);
  12. if (count != null &amp;&amp; count &gt; 0) {
  13. map1.put(n,count-1);
  14. inter.add(n);
  15. }
  16. }
  17. int[] output = new int[inter.size()];
  18. for(int i = 0; i &lt; inter.size(); i++)
  19. output[i]=inter.get(i);
  20. return output;
  21. }

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:

确定