|
5 | 5 | import java.util.Set; |
6 | 6 | import java.util.stream.Collectors; |
7 | 7 |
|
8 | | -/** |
9 | | - * 349. Intersection of Two Arrays |
10 | | - * |
11 | | - * Given two arrays, write a function to compute their intersection. |
12 | | - * |
13 | | - * Example 1: |
14 | | - * Input: nums1 = [1,2,2,1], nums2 = [2,2] |
15 | | - * Output: [2] |
16 | | - * |
17 | | - * Example 2: |
18 | | - * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] |
19 | | - * Output: [9,4] |
20 | | - * |
21 | | - * Note: |
22 | | - * Each element in the result must be unique. |
23 | | - * The result can be in any order. |
24 | | - */ |
25 | 8 | public class _349 { |
26 | 9 |
|
27 | | - public static class Solution1 { |
28 | | - //credit: https://leetcode.com/articles/intersection-of-two-arrays/ |
29 | | - //Time: O(m+n) on average, O(m*n) in worse case |
30 | | - //Space: O(m+n) |
31 | | - public int[] intersection(int[] nums1, int[] nums2) { |
32 | | - Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); |
33 | | - Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); |
34 | | - set1.retainAll(set2); |
35 | | - int[] intersection = new int[set1.size()]; |
36 | | - int i = 0; |
37 | | - for (int num : set1) { |
38 | | - intersection[i++] = num; |
39 | | - } |
40 | | - return intersection; |
| 10 | + public static class Solution1 { |
| 11 | + //credit: https://leetcode.com/articles/intersection-of-two-arrays/ |
| 12 | + //Time: O(m+n) on average, O(m*n) in worse case |
| 13 | + //Space: O(m+n) |
| 14 | + public int[] intersection(int[] nums1, int[] nums2) { |
| 15 | + Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); |
| 16 | + Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet()); |
| 17 | + set1.retainAll(set2); |
| 18 | + int[] intersection = new int[set1.size()]; |
| 19 | + int i = 0; |
| 20 | + for (int num : set1) { |
| 21 | + intersection[i++] = num; |
| 22 | + } |
| 23 | + return intersection; |
| 24 | + } |
41 | 25 | } |
42 | | - } |
43 | 26 |
|
44 | | - public static class Solution2 { |
45 | | - //Time: O(nlgn) |
46 | | - public int[] intersection(int[] nums1, int[] nums2) { |
47 | | - Arrays.sort(nums2); |
48 | | - Set<Integer> intersect = new HashSet(); |
49 | | - for (int i : nums1) { |
50 | | - if (binarySearch(i, nums2)) { |
51 | | - intersect.add(i); |
| 27 | + public static class Solution2 { |
| 28 | + //Time: O(nlgn) |
| 29 | + public int[] intersection(int[] nums1, int[] nums2) { |
| 30 | + Arrays.sort(nums2); |
| 31 | + Set<Integer> intersect = new HashSet(); |
| 32 | + for (int i : nums1) { |
| 33 | + if (binarySearch(i, nums2)) { |
| 34 | + intersect.add(i); |
| 35 | + } |
| 36 | + } |
| 37 | + int[] result = new int[intersect.size()]; |
| 38 | + int i = 0; |
| 39 | + for (int num : intersect) { |
| 40 | + result[i++] = num; |
| 41 | + } |
| 42 | + return result; |
52 | 43 | } |
53 | | - } |
54 | | - int[] result = new int[intersect.size()]; |
55 | | - int i = 0; |
56 | | - for (int num : intersect) { |
57 | | - result[i++] = num; |
58 | | - } |
59 | | - return result; |
60 | | - } |
61 | 44 |
|
62 | | - private boolean binarySearch(int i, int[] nums) { |
63 | | - int left = 0; |
64 | | - int right = nums.length - 1; |
65 | | - while (left <= right) { |
66 | | - int mid = left + (right - left) / 2; |
67 | | - if (nums[mid] == i) { |
68 | | - return true; |
69 | | - } else if (nums[mid] > i) { |
70 | | - right = mid - 1; |
71 | | - } else { |
72 | | - left = mid + 1; |
| 45 | + private boolean binarySearch(int i, int[] nums) { |
| 46 | + int left = 0; |
| 47 | + int right = nums.length - 1; |
| 48 | + while (left <= right) { |
| 49 | + int mid = left + (right - left) / 2; |
| 50 | + if (nums[mid] == i) { |
| 51 | + return true; |
| 52 | + } else if (nums[mid] > i) { |
| 53 | + right = mid - 1; |
| 54 | + } else { |
| 55 | + left = mid + 1; |
| 56 | + } |
| 57 | + } |
| 58 | + return false; |
73 | 59 | } |
74 | | - } |
75 | | - return false; |
76 | 60 | } |
77 | | - } |
78 | 61 |
|
79 | | - public static class Solution3 { |
80 | | - //use two pointers |
81 | | - //credit: https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions |
82 | | - public int[] intersection(int[] nums1, int[] nums2) { |
83 | | - Arrays.sort(nums1); |
84 | | - Arrays.sort(nums2); |
85 | | - int i = 0; |
86 | | - int j = 0; |
87 | | - Set<Integer> set = new HashSet<>(); |
88 | | - while (i < nums1.length && j < nums2.length) { |
89 | | - if (nums1[i] == nums2[j]) { |
90 | | - set.add(nums1[i++]); |
91 | | - j++; |
92 | | - } else if (nums1[i] < nums2[j]) { |
93 | | - i++; |
94 | | - } else { |
95 | | - j++; |
| 62 | + public static class Solution3 { |
| 63 | + //use two pointers |
| 64 | + //credit: https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions |
| 65 | + public int[] intersection(int[] nums1, int[] nums2) { |
| 66 | + Arrays.sort(nums1); |
| 67 | + Arrays.sort(nums2); |
| 68 | + int i = 0; |
| 69 | + int j = 0; |
| 70 | + Set<Integer> set = new HashSet<>(); |
| 71 | + while (i < nums1.length && j < nums2.length) { |
| 72 | + if (nums1[i] == nums2[j]) { |
| 73 | + set.add(nums1[i++]); |
| 74 | + j++; |
| 75 | + } else if (nums1[i] < nums2[j]) { |
| 76 | + i++; |
| 77 | + } else { |
| 78 | + j++; |
| 79 | + } |
| 80 | + } |
| 81 | + int[] intersection = new int[set.size()]; |
| 82 | + int k = 0; |
| 83 | + for (int num : set) { |
| 84 | + intersection[k++] = num; |
| 85 | + } |
| 86 | + return intersection; |
96 | 87 | } |
97 | | - } |
98 | | - int[] intersection = new int[set.size()]; |
99 | | - int k = 0; |
100 | | - for (int num : set) { |
101 | | - intersection[k++] = num; |
102 | | - } |
103 | | - return intersection; |
104 | 88 | } |
105 | | - } |
106 | 89 | } |
0 commit comments