# 两数组的交

1. 集合

2. 哈希表

3. 排序+两根指针

class Solution:
# @param {int[]} nums1 an integer array
# @param {int[]} nums2 an integer array
# @return {int[]} an integer array
def intersection(self, nums1, nums2):
return list(set(s1) & set(s2))
# Write your code here

class Solution:
# @param {int[]} nums1 an integer array
# @param {int[]} nums2 an integer array
# @return {int[]} an integer array
def intersection(self, nums1, nums2):
hash_table = {}
result = []
hash_table.fromkeys(nums1)
for i in nums2:
if len(hash_table) == 0:
break
if i in hash_table:
result.append(i)
hash_table.pop(i)
return result
# Write your code here

class Solution:
# @param {int[]} nums1 an integer array
# @param {int[]} nums2 an integer array
# @return {int[]} an integer array
def intersection(self, nums1, nums2):
result = []
nums1.sort()
nums2.sort()
index1, index2 = 0, 0
n1, n2 = len(nums1), len(nums2)

if n1 == 0 or n2 == 0:
return []

while index1 < n1 and index2 < n2:
if nums1[index1] == nums2[index2]:
temp = nums1[index1]
result.append(temp)
while index1 < n1 and nums1[index1] == temp:
index1 += 1
while index2 < n2 and nums2[index2] == temp:
index2 += 1
elif nums1[index1] > nums2[index2]:
index2 += 1
else:
index1 += 1
return result
# Write your code here

举报原因： 您举报文章：两数组的交 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)