题目描述
Given two arrays, write a function to compute their intersection.
Example 1:
1 |
Input: nums1 = [1,2,2,1], nums2 = [2,2] |
Example 2:
1 |
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
这里我使用了一个dict来存放nums1中的数字,其中key为nums1的数字,value为该key出现在nums1中的次数。然后遍历nums2,并且在dict中查找是否出现过这个key,如果有,则给该key的value-=1,并且在result中加入一次这个key,如果value变成了0,就pop这个key。
代码实现
1 |
class : |
近期评论