물음표 살인마의 개발블로그

알고리즘 문제/Leetcode

349. Intersection of Two Arrays

BEstyle 2023. 1. 12. 00:06

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.

 

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

# https://leetcode.com/problems/intersection-of-two-arrays/
'''
1. 아이디어 :
    1) set으로 중복을 제거한 후, 두개의 set을 비교하여 교집합을 구한다.
2. 시간복잡도 :
    1) O(n)
3. 자료구조 :
    1) Set

'''

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))

'알고리즘 문제 > Leetcode' 카테고리의 다른 글

160. Intersection of Two Linked Lists  (0) 2023.01.17
240. Search a 2D Matrix II  (0) 2023.01.17
278. First Bad Version  (0) 2023.01.12
268. Missing Number  (0) 2023.01.12
259. 3Sum Smaller  (0) 2023.01.12