【题目描述】
Given two arrays, write a function to compute their intersection.
Example
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Notice
Each element in the result must be unique.
The result can be in any order.
给定两个数组,编写函数计算它们的交集。
举例:给定数组 nums1 = [1, 2, 2, 1], nums2 = [2, 2],应该返回 [2]。
注意事项:
1. 结果中的每个元素一定是唯一的。
2. 结果可以采用任意顺序。
【题目链接】
www.lintcode.com/en/old/problem/intersection-of-two-arrays/
【题目解析】
这道题让我们找两个数组交集的部分(不包含重复数字),难度不算大,我们可以用个set把nums1都放进去,然后遍历nums2的元素,如果在set中存在,说明是交集的部分,加入结果的set中,最后再把结果转为vector的形式即可。
【参考答案】
网友评论