leetcode 349[easy]---Intersection of Two Arrays

来源:互联网 发布:创世数据网 编辑:程序博客网 时间:2024/06/15 01:03

难度:easy

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.
思路:给定两个list,求两个list的交集,相同的数字只需要出现一次,不能重复。
         方法1:用set的”可以求交集“和”set内的元素不能重复“这两个特性解题。
         方法2:for循环找到存在list1但不存在于list2中,且不重复的数字。两个方法都很妙。