標籤:

DAY45:Intersection of Two Arrays

題目:

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.

思路:

依次判斷List1中的元素是否在List2中出現,如果出現則將其加入到返回列表中。最後通過集合類型的數據的特性,將返回列表中重複元素去掉。

代碼:

class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ ret=[] for i in nums1: if i in nums2: ret+=[i] ret=list(set(ret)) return ret

推薦閱讀:

Leetcode-38-Count-and-Say(學渣的痛大神們不懂。。。)
LeetCode 簡略題解 - 1~100
leetcode-1
LeetCode 簡略題解 - 301~400
[leetcode algorithms]題目1

TAG:Python | LeetCode |