본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode 350. Intersection of Two Arrays II

by 벤진[Benzene] 2020. 5. 24.

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]

https://leetcode.com/

댓글