Problem:
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
-Summary-
Using the dictionary
1. Create a dictionary to hold an all numbers as a 'key' in the first nums1 list (Count +1 for each key)
2. Loop the second nums2 list and only append to answer list if the number is in the dictionary and count is still bigger than zero.
뭔가 계속 헤메다가 Leetcode의 discussion을 보고 참조하여 아이디어를 얻고 답을 작성하였다. 문제가 안풀릴땐 Leetcode의 discussion이 많은 도움이 된다.
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode 387. First Unique Character in a String (0) | 2020.05.25 |
---|---|
LeetCode 7. Reverse Integer (0) | 2020.05.25 |
LeetCode 136. Single Number (0) | 2020.05.23 |
LeetCode 217. Contains Duplicate (0) | 2020.05.23 |
LeetCode 122. Best Time to Buy and Sell Stock II (1) | 2020.05.23 |
댓글