forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_349.java
82 lines (77 loc) · 2.01 KB
/
_349.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* 349. Intersection of Two Arrays
*
* 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.
*/
public class _349 {
public static class Solution1 {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
for (; i < nums1.length && j < nums2.length; ) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
set.add(nums1[i]);
i++;
j++;
}
}
int[] result = new int[set.size()];
Iterator<Integer> it = set.iterator();
int k = 0;
while (it.hasNext()) {
result[k++] = it.next();
}
return result;
}
}
public static class Solution2 {
public int[] intersection(int[] nums1, int[] nums2) {
//this approach is O(nlgn)
Arrays.sort(nums1);
Arrays.sort(nums2);
Set<Integer> intersect = new HashSet();
for (int i : nums1) {
if (binarySearch(i, nums2)) {
intersect.add(i);
}
}
int[] result = new int[intersect.size()];
Iterator<Integer> it = intersect.iterator();
for (int i = 0; i < intersect.size(); i++) {
result[i] = it.next();
}
return result;
}
private boolean binarySearch(int i, int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == i) {
return true;
} else if (nums[mid] > i) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return false;
}
}
}