349. Intersection of Two Arrays

来源:互联网 发布:淘宝怎么做免费推广 编辑:程序博客网 时间:2024/06/01 08:42

题目摘要
写一个函数找到两个给定数组的相同元素,返回结果中每个元素只出现一次

解法
* HashMap,但是时间较长
* 先将两个数组排序,再逐项比较

注意
1. HashMap的用法
2. HashMap不能直接转换为数组
3. 排序法时,现新建一个nums1数组长度的临时存储数组,最后搬移到另一个正确长度的数组中

可问问题

原题
Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.
0 0
原创粉丝点击