Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
- Each element in the result must be unique.
- The result can be in any order.
https://discuss.leetcode.com/topic/45685/three-java-solutions
** 解題思路**
三種解法:
Use two hash sets , O(n) Time complexity
先把nums1加入到set中,然后用nums2中元素在set中遍歷,如果set包含該元素儒鹿,就加入到intersect set中唬涧。最后把intersect set 存入結(jié)果數(shù)組。Sort both arrays, use two pointers O(nlogn) Time complexity
Binary search O(nlogn) Time complexity
先sort nums2 宫补,然后用nums1元素在nums2中做binary search檬姥。如果能找到,就加入結(jié)果集守谓。