349. 两个数组的交集

发布时间 2023-05-05 15:29:58作者: 猥琐丑八怪

 分析:

本来刷二分的,但是这道题不用双指针也能做

所以就偷个懒,加上数组范围小,遍历就行

代码:

 1 class Solution(object):
 2     def intersection(self, nums1, nums2):
 3         """
 4         :type nums1: List[int]
 5         :type nums2: List[int]
 6         :rtype: List[int]
 7         """
 8         count=[]
 9         if len(nums1)<len(nums2):
10             for i in nums1:
11                 if i in nums2 and i not in count:
12                     count.append(i)
13         elif len(nums1)>len(nums2):
14             for i in nums2:
15                 if i in nums1 and i not in count:
16                     count.append(i)
17         else:
18             for i in nums1:
19                 if i in nums2 and i not in count:
20                     count.append(i)
21         return count