[LeetCode/Python] 349. Intersection of Two Arrays
2025. 4. 21. 17:54
99클럽 코테 스터디 | 비기너 | 16일차
https://leetcode.com/problems/intersection-of-two-arrays/description/
접근 방법
1. list 형태의 nums1과 nums2을 set으로 바꿈
2. nums1의 원소들에 대하여 nums2에 존재할 경우 set의 형태인 intersection_num에 추가함
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
intersection_num = set()
nums1_set = sorted(set(nums1))
nums2_set = sorted(set(nums2))
for num in nums1_set:
if num in nums2_set:
intersection_num.add(num)
return list(intersection_num)
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode/Python] 1385. Find the Distance Value Between Two Arrays (0) | 2025.04.23 |
---|---|
[LeetCode/Python] 187. Repeated DNA Sequences (0) | 2025.04.14 |
[LeetCode/Python] 1464. Maximum Product of Two Elements in an Array (0) | 2025.04.10 |
[LeetCode/Python] 706. Design HashMap (0) | 2025.04.10 |
[LeetCode/Python] 2283. Check if Number Has Equal Digit Count and Digit Value (0) | 2025.04.09 |