Leetcode 349 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.
class Solution(object):
    def intersection(self, nums1, nums2):
        h, ans = {}, []
        for x in nums1:
            h[x] = 1
        for x in nums2:
            if h.has_key(x):
                ans.append(x)
                del h[x]
        return ans

 

posted @ 2016-05-19 15:06  lilixu  阅读(258)  评论(0编辑  收藏  举报