Leetcode 349. Intersection of Two Arrays

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Intersection of Two Arrays

2. Solution

  • Version 1
1
2
3
4
5
6
7
class Solution:
def intersection(self, nums1, nums2):
result = set()
for x in nums1:
if x in nums2:
result.add(x)
return list(result)
  • Version 2
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def intersection(self, nums1, nums2):
result = set()
stat = {}
for x in nums1:
stat[x] = x

for x in nums2:
if x in stat:
result.add(x)

return list(result)
  • Version 3
1
2
3
class Solution:
def intersection(self, nums1, nums2):
return list(set(nums1) & set(nums2))

Reference

  1. https://leetcode.com/problems/intersection-of-two-arrays/
如果有收获,可以请我喝杯咖啡!