Leetcode 349. Intersection of Two Arrays

Problem

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Algorithm

Collect all the elements in each list in two visited list and then return the items appear in both visited list.

Code

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        item1 = [0] * 1001
        item2 = [0] * 1001
        for i in nums1:
            item1[i] = 1
        for i in nums2:
            item2[i] = 1

        ans = []
        for i in range(1001):
            if item1[i] and item2[i]:
                ans.append(i)
        return ans

相关推荐

  1. Leetcode 344. Reverse String

    2023-12-08 14:14:03       57 阅读
  2. LeetCode-394.字符串解码】

    2023-12-08 14:14:03       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 14:14:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 14:14:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 14:14:03       87 阅读
  4. Python语言-面向对象

    2023-12-08 14:14:03       96 阅读

热门阅读

  1. Linux篇之基于Centos的everything镜像搭建yum镜像源

    2023-12-08 14:14:03       61 阅读
  2. WordPress禁止显示指定类别的文章

    2023-12-08 14:14:03       60 阅读
  3. Elasticsearch桶聚合和管道聚合

    2023-12-08 14:14:03       47 阅读
  4. Docker

    Docker

    2023-12-08 14:14:03      64 阅读
  5. 超详细数学建模论文模板分享

    2023-12-08 14:14:03       52 阅读