LC打怪录Day6哈希表(1.2)-349. 两个数组的交集

给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的

method 1: array 数组解

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # 创建两个长度为1001的列表count1和count2,初始值都是0。
        # 这里假设数组中的数字不会超过1000。
        count1 = [0]*1001
        count2 = [0]*1001

        # 创建一个空列表result,用于存储最终的交集结果。
        result = []

        # 遍历第一个数组nums1。
        # 对于数组中的每个元素nums1[i],将count1中相应位置的计数加1。
        # 例如,如果nums1[i]是3,那么count1[3]就加1。
        for i in range(len(nums1)):
            count1[nums1[i]] += 1

        # 以同样的方法遍历第二个数组nums2,更新count2。
        for j in range(len(nums2)):
            count2[nums2[j]] += 1

        # 遍历从0到1000的所有数字。
        # 如果某个数字在两个数组中都出现过(即count1[k]和count2[k]都大于0),
        # 那么就把这个数字加入到结果列表result中。
        for k in range(1001):
            if count1[k] * count2[k] > 0:
                result.append(k)

        # 返回结果列表。
        return result

这段代码的核心思想是使用两个计数数组来跟踪每个数组中各个数字出现的次数。然后,通过比较这两个计数数组,找出同时在两个数组中出现的数字。这种方法避免了直接比较两个数组的元素,从而降低了算法的复杂度。

  1. Initializing Count Arrays: The time complexity for initializing count1 and count2 is O(1) since it's a fixed size (1001).
  2. First for-loop (nums1): The loop runs for each element in nums1, so its time complexity is O(N), where N is the length of nums1.
  3. Second for-loop (nums2): Similarly, this loop's time complexity is O(M), where M is the length of nums2.
  4. Third for-loop: This loop is independent of the input arrays' lengths and always iterates 1001 times, so its time complexity is O(1).

Overall, the first version of the code has a time complexity of O(N + M), where N and M are the lengths of nums1 and nums2, respectively.

method 2: 合集 set

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # 使用Python内置的set函数将两个数组转换成集合。
        # 集合是一种数据结构,它只存储唯一的元素,自动去除重复的部分。
        # 然后使用 & 运算符找出两个集合的交集。
        # 交集包含了同时存在于nums1和nums2中的元素。
        return list(set(nums1) & set(nums2))

这个方法的核心是利用集合(set)的特性。集合自动处理重复元素,并且提供了方便的方法来找出两个集合的共同元素(即交集)。这种方法的代码更加简洁,易于理解,同时也有很好的性能,特别是当数组较大时。通过转换成集合并使用集合的交集操作,可以有效地减少比较的次数,从而提高算法的效率。

  1. Converting to Sets: Converting nums1 and nums2 to sets has a time complexity of O(N) and O(M), respectively, since each element in the array needs to be added to the set.
  2. Intersection: The intersection operation for sets generally has a time complexity of O(min(N, M)) since it involves checking elements of the smaller set against the larger set.

Thus, the second version of the code has a time complexity of O(N + M + min(N, M)), which simplifies to O(N + M).

In summary, both versions have a similar overall time complexity of O(N + M). However, the second version might have some additional hidden constants due to the internal workings of set operations in Python, but it's more concise and likely to be faster in practice due to set operations being highly optimized.

  • 22
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值