标签:solution highlight res cti code intersect lse tco pen
排序。更naive的方法是用set。
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [] nums1.sort() nums2.sort() i = j = 0 while i < len(nums1) and j < len(nums2): if nums1[i] == nums2[j]: if nums1[i] not in result: result.append(nums1[i]) i += 1 elif nums1[i] < nums2[j]: i += 1 else: # nums1[i] > nums2[j] j += 1 return result
[leetcode]Intersection of Two Arrays
标签:solution highlight res cti code intersect lse tco pen
原文地址:https://www.cnblogs.com/lautsie/p/12267648.html