首页 > 其他分享 >LeetCode 2956. Find Common Elements Between Two Arrays

LeetCode 2956. Find Common Elements Between Two Arrays

时间:2024-05-14 09:20:13浏览次数:14  
标签:Elements HashMap Arrays res Two two int nums1 nums2

原题链接在这里:https://leetcode.com/problems/find-common-elements-between-two-arrays/description/

题目:

You are given two 0-indexed integer arrays nums1 and nums2 of sizes n and m, respectively.

Consider calculating the following values:

  • The number of indices i such that 0 <= i < n and nums1[i] occurs at least once in nums2.
  • The number of indices i such that 0 <= i < m and nums2[i] occurs at least once in nums1.

Return an integer array answer of size 2 containing the two values in the above order.

Example 1:

Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
Output: [3,4]
Explanation: We calculate the values as follows:
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.

Example 2:

Input: nums1 = [3,4,2,3], nums2 = [1,5]
Output: [0,0]
Explanation: There are no common elements between the two arrays, so the two values will be 0.

Constraints:

  • n == nums1.length
  • m == nums2.length
  • 1 <= n, m <= 100
  • 1 <= nums1[i], nums2[i] <= 100

题解:

Accumulate the freq of each num into HashMap.

For the keys in the first map, att the its corresponding freq from the second map to res[1].

Vice versa.

Time Complexity: O(m + n). m = nums1.length. n = nums2.length.

Space: O(m + n).

AC Java:

 1 class Solution {
 2     public int[] findIntersectionValues(int[] nums1, int[] nums2) {
 3         int[] res = new int[2];
 4         Map<Integer, Integer> hm1 = getCount(nums1);
 5         Map<Integer, Integer> hm2 = getCount(nums2);
 6         for(int key : hm1.keySet()){
 7             res[1] += hm2.getOrDefault(key, 0);
 8         }
 9 
10         for(int key : hm2.keySet()){
11             res[0] += hm1.getOrDefault(key, 0);
12         }
13 
14         return res;
15     }
16 
17     private HashMap<Integer, Integer> getCount(int[] nums){
18         HashMap<Integer, Integer> hm = new HashMap<>();
19         for(int num : nums){
20             hm.put(num, hm.getOrDefault(num, 0) + 1);
21         }
22 
23         return hm;
24     }
25 }

 

标签:Elements,HashMap,Arrays,res,Two,two,int,nums1,nums2
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18190538

相关文章

  • HTML 04 - Elements
    HTMLElementsarebuildingblockofawebpage.Itisusedtocreatecomponentforwebpages.HTMLdocumentsconsistofatreeoftheseelementsandtheyspecifyhowHTMLdocumentsshouldbebuilt,andwhatkindofcontentshouldbeplacedwithinvariousp......
  • Find Products of Elements of Big Array
    FindProductsofElementsofBigArrayA powerfularray foraninteger x istheshortestsortedarrayofpowersoftwothatsumupto x.Forexample,thepowerfularrayfor11is [1,2,8].Thearray big_nums iscreatedbyconcatenatingthe powerful......
  • KAN: Kolmogorov–Arnold Networks 学术论文全译
    KAN:Kolmogorov–ArnoldNetworks学术论文全译来源 https://zhuanlan.zhihu.com/p/696001648 KAN:Kolmogorov–ArnoldNetworks https://arxiv.org/pdf/2404.19756讨论Applicationaspects:WehavepresentedsomepreliminaryevidencesthatKANsaremoreeffective......
  • 使用Arrays.asList()的坑
    背景在将数组转为list的时候,一般会使用到Arrays.asList()这个方法,但是在对转化后的list进行add操作的时候出现了java.lang.UnsupportedOperationException的报错原因Arrays.asList()方法只是将数组转换为一个固定长度的列表,它不支持增删操作。研究源码发现,它生成的ArrayLis......
  • CF207C3 Game with Two Trees
    CF207C3GamewithTwoTrees妙到家的树上字符串问题。约定树\(1\):\(t_1\)。树\(2\):\(t_2\)。\(S_{1/2}(i,l)\)为树\(1/2\)上节点\(i\)沿父亲走经过\(l\)​条边所构成的字符串。\(E_{1/2}(u,v)\)为树\(1/2\)上,连接节点\(u,v\)​的边的字符。\(fa_{......
  • 慎用 Arrays.asList
    Java8提供的Stream流式处理大大减少了集合类各种操作(投影、过滤、转换)的代码量,用起来非常香,所以在实际业务开发中,我们常常会把原始的数组转换为List类数据结构,使得其可以用上Stream流操作。Arrays.asList方法应该是各位最常用的数组一键转换为List的方法了,但这个方法......
  • 网络策略_NetworkPolicy
    网络策略NetworkPolicy前端通过访问后端的API接口来获取数据库中的数据前端、API接口、数据库在k8s中都为podk8s要求所有pod可以互相访问,但生产中,不能让前端直接访问后端数据库目标是保护数据库,使其不允许从除了API以外的任何pod访问apiVersion:networking.k8s.io/v1kind:......
  • Outrageously Large Neural Networks The Sparsely-Gated Mixture-of-Experts Layer
    目录概MoE训练ShazeerN.,MirhoseiniA.,MaziarzK.,DavisA.,LeQ.,HintonG.andDeanJ.Outrageouslylargeneuralnetworks:Thesparsely-gatedmixture-of-expertslayer.ICLR,2017.概Mixture-of-Experts(MoE).MoE通过一gatingnetwork选择不同的exp......
  • 第一章:Introduction to Interconnection Networks
    第一章:互联网络介绍数字系统三个基本组件逻辑(logic)存储(memory)通信(communication)随着技术进步,当前大部分数字芯片系统的瓶颈在于通信,组件之间的通信偏绿远远落后于处理器时钟频率,因此互联是未来数字系统成功的关键因素。关于互联网络的三个问题什么是互联网络互连网络是......
  • GitHub two-factor authentication开启教程
    问题描述最近登录GitHub个人页面动不动就有一个提示框”......two-factorauthenticationwillberequiredforyouraccountstartingJan4,2024......“,点击去看了一下原来是GitHub对所有的用户登录都要开启双重身份认证,要在1月4号前完成解决办法GitHub个人页面点击右......