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 # 保证s1短
9 if len(nums1) > len(nums2):
10 nums1, nums2 = nums2, nums1
11 # 返回值
12 res = [int] * len(nums1)
13 # res下标
14 index = 0
15 # 遍历s1
16 i = 0
17 while i < len(nums1):
18 # 在s2中,不在res中,则添加到res中,下标加1,指针后移
19 if nums1[i] in nums2 and nums1[i] not in res:
20 res[index] = nums1[i]
21 index += 1
22 i += 1
23 # 指针后移
24 else:
25 i += 1
26 return res[0:index]
27
28
29 if __name__ == '__main__':
30 solution = Solution()
31 print(solution.intersection(nums1=[4, 9, 5], nums2=[9, 4, 9, 8, 4]))
32 print(solution.intersection(nums1=[1, 2, 2, 1], nums2=[2, 2]))