Skip to content

Commit d8a50e0

Browse files
refactor 349
1 parent 69ffc4f commit d8a50e0

File tree

1 file changed

+69
-137
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+69
-137
lines changed

src/main/java/com/fishercoder/solutions/_349.java

Lines changed: 69 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -5,146 +5,78 @@
55
import java.util.Iterator;
66
import java.util.Set;
77

8-
/**349. Intersection of Two Arrays
8+
/**
9+
* 349. Intersection of Two Arrays
910
*
10-
Given two arrays, write a function to compute their intersection.
11-
12-
Example:
13-
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
14-
15-
Note:
16-
Each element in the result must be unique.
17-
The result can be in any order.*/
11+
* Given two arrays, write a function to compute their intersection.
12+
*
13+
* Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
14+
*
15+
* Note: Each element in the result must be unique. The result can be in any order.
16+
*/
1817
public class _349 {
1918

20-
//then I clicked its Tags, and find it's marked with so many tags: Binary Search, HashTable, Two Pointers, Sort, now I'll try to do it one by one
21-
//inspired by this post: https://discuss.leetcode.com/topic/45685/three-java-solutions
22-
public int[] intersection_two_pointers(int[] nums1, int[] nums2) {
23-
Set<Integer> set = new HashSet();
24-
Arrays.sort(nums1);
25-
Arrays.sort(nums2);
26-
int i = 0;
27-
int j = 0;
28-
for (; i < nums1.length && j < nums2.length; ) {
29-
if (nums1[i] < nums2[j]) {
30-
i++;
31-
} else if (nums1[i] > nums2[j]) {
32-
j++;
33-
} else {
34-
set.add(nums1[i]);
35-
i++;
36-
j++;
37-
}
38-
}
39-
int[] result = new int[set.size()];
40-
Iterator<Integer> it = set.iterator();
41-
int k = 0;
42-
while (it.hasNext()) {
43-
result[k++] = it.next();
44-
}
45-
return result;
46-
}
47-
48-
public int[] intersection_binary_search(int[] nums1, int[] nums2) {
49-
//this approach is O(nlgn)
50-
Arrays.sort(nums1);
51-
Arrays.sort(nums2);
52-
Set<Integer> intersect = new HashSet();
53-
for (int i : nums1) {
54-
if (binarySearch(i, nums2)) {
55-
intersect.add(i);
56-
}
57-
}
58-
int[] result = new int[intersect.size()];
59-
Iterator<Integer> it = intersect.iterator();
60-
for (int i = 0; i < intersect.size(); i++) {
61-
result[i] = it.next();
62-
}
63-
return result;
64-
}
65-
66-
private boolean binarySearch(int i, int[] nums) {
67-
int left = 0;
68-
int right = nums.length - 1;
69-
while (left <= right) {
70-
int mid = left + (right - left) / 2;
71-
if (nums[mid] == i) {
72-
return true;
73-
} else if (nums[mid] > i) {
74-
right = mid - 1;
75-
} else {
76-
left = mid + 1;
77-
}
78-
}
79-
return false;
80-
}
81-
82-
//tried a friend's recommended approach, didn't finish it to get it AC'ed, turned to normal approach as above and got it AC'ed.
83-
private boolean binarySearch_not_working_version(int i, int[] nums) {
84-
if (nums == null || nums.length == 0) {
85-
return false;
86-
}
87-
int left = 0;
88-
int right = nums.length - 1;
89-
while (left + 1 < right) {
90-
int mid = left + (right - left) / 2;
91-
if (nums[mid] > i) {
92-
right = mid;
93-
} else if (nums[mid] < 1) {
94-
left = mid;
95-
} else if (nums[mid] == i) {
96-
return true;
97-
} else {
98-
return false;
99-
}
100-
}
101-
return nums[left] == i || nums[right] == i;
102-
}
103-
104-
public static void main(String... strings) {
105-
_349 test = new _349();
106-
int[] nums1 = new int[]{1, 2};
107-
int[] nums2 = new int[]{2, 1};
108-
test.intersection_binary_search(nums1, nums2);
109-
}
110-
111-
public int[] intersection_two_hashsets(int[] nums1, int[] nums2) {
112-
//this approach is O(n)
113-
Set<Integer> set1 = new HashSet();
114-
for (int i = 0; i < nums1.length; i++) {
115-
set1.add(nums1[i]);
116-
}
117-
Set<Integer> intersect = new HashSet();
118-
for (int i = 0; i < nums2.length; i++) {
119-
if (set1.contains(nums2[i])) {
120-
intersect.add(nums2[i]);
121-
}
122-
}
123-
int[] result = new int[intersect.size()];
124-
Iterator<Integer> it = intersect.iterator();
125-
for (int i = 0; i < intersect.size(); i++) {
126-
result[i] = it.next();
127-
}
128-
return result;
129-
}
19+
public static class Solution1 {
20+
public int[] intersection(int[] nums1, int[] nums2) {
21+
Set<Integer> set = new HashSet();
22+
Arrays.sort(nums1);
23+
Arrays.sort(nums2);
24+
int i = 0;
25+
int j = 0;
26+
for (; i < nums1.length && j < nums2.length; ) {
27+
if (nums1[i] < nums2[j]) {
28+
i++;
29+
} else if (nums1[i] > nums2[j]) {
30+
j++;
31+
} else {
32+
set.add(nums1[i]);
33+
i++;
34+
j++;
35+
}
36+
}
37+
int[] result = new int[set.size()];
38+
Iterator<Integer> it = set.iterator();
39+
int k = 0;
40+
while (it.hasNext()) {
41+
result[k++] = it.next();
42+
}
43+
return result;
44+
}
45+
}
13046

131-
//so naturally, I come up with this naive O(n^2) solution and surprisingly it got AC'ed immediately, no wonder it's marked as EASY.
132-
public int[] intersection_naive(int[] nums1, int[] nums2) {
133-
Set<Integer> set = new HashSet();
134-
for (int i = 0; i < nums1.length; i++) {
135-
for (int j = 0; j < nums2.length; j++) {
136-
if (nums1[i] == nums2[j]) {
137-
set.add(nums1[i]);
138-
}
139-
}
140-
}
141-
int[] result = new int[set.size()];
142-
Iterator<Integer> it = set.iterator();
143-
int i = 0;
144-
while (it.hasNext()) {
145-
result[i++] = it.next();
146-
}
147-
return result;
148-
}
47+
public static class Solution2 {
48+
public int[] intersection(int[] nums1, int[] nums2) {
49+
//this approach is O(nlgn)
50+
Arrays.sort(nums1);
51+
Arrays.sort(nums2);
52+
Set<Integer> intersect = new HashSet();
53+
for (int i : nums1) {
54+
if (binarySearch(i, nums2)) {
55+
intersect.add(i);
56+
}
57+
}
58+
int[] result = new int[intersect.size()];
59+
Iterator<Integer> it = intersect.iterator();
60+
for (int i = 0; i < intersect.size(); i++) {
61+
result[i] = it.next();
62+
}
63+
return result;
64+
}
14965

66+
private boolean binarySearch(int i, int[] nums) {
67+
int left = 0;
68+
int right = nums.length - 1;
69+
while (left <= right) {
70+
int mid = left + (right - left) / 2;
71+
if (nums[mid] == i) {
72+
return true;
73+
} else if (nums[mid] > i) {
74+
right = mid - 1;
75+
} else {
76+
left = mid + 1;
77+
}
78+
}
79+
return false;
80+
}
81+
}
15082
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy