|
1 | 1 | package easy;
|
2 | 2 |
|
| 3 | +import java.util.Arrays; |
3 | 4 | import java.util.HashSet;
|
4 | 5 | import java.util.Iterator;
|
5 | 6 | import java.util.Set;
|
|
19 | 20 | public class IntersectionOfTwoArrays {
|
20 | 21 |
|
21 | 22 | //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
|
22 |
| - |
| 23 | + //inspired by this post: https://discuss.leetcode.com/topic/45685/three-java-solutions |
| 24 | + public int[] intersection_two_pointers(int[] nums1, int[] nums2) { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + public int[] intersection_binary_search(int[] nums1, int[] nums2) { |
| 29 | + //this approach is O(nlgn) |
| 30 | + Arrays.sort(nums1); |
| 31 | + Arrays.sort(nums2); |
| 32 | + Set<Integer> intersect = new HashSet(); |
| 33 | + for(int i : nums1){ |
| 34 | + if(binarySearch(i, nums2)){ |
| 35 | + intersect.add(i); |
| 36 | + } |
| 37 | + } |
| 38 | + int[] result = new int[intersect.size()]; |
| 39 | + Iterator<Integer> it = intersect.iterator(); |
| 40 | + for(int i = 0; i < intersect.size(); i++){ |
| 41 | + result[i] = it.next(); |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + private boolean binarySearch(int i, int[] nums) { |
| 47 | + int left = 0, 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; |
| 59 | + } |
| 60 | + |
| 61 | + //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. |
| 62 | + private boolean binarySearch_not_working_version(int i, int[] nums) { |
| 63 | + if(nums == null || nums.length == 0) return false; |
| 64 | + int left = 0, right = nums.length-1; |
| 65 | + while(left+1 < right){ |
| 66 | + int mid = left + (right-left)/2; |
| 67 | + if(nums[mid] > i){ |
| 68 | + right = mid; |
| 69 | + } else if(nums[mid] < 1){ |
| 70 | + left = mid; |
| 71 | + } else if(nums[mid] == i){ |
| 72 | + return true; |
| 73 | + } else { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + return nums[left] == i || nums[right] == i; |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String...strings){ |
| 81 | + IntersectionOfTwoArrays test = new IntersectionOfTwoArrays(); |
| 82 | + int[] nums1 = new int[]{1,2}; |
| 83 | + int[] nums2 = new int[]{2,1}; |
| 84 | + test.intersection_binary_search(nums1 , nums2); |
| 85 | + } |
| 86 | + |
| 87 | + public int[] intersection_two_hashsets(int[] nums1, int[] nums2) { |
| 88 | + //this approach is O(n) |
| 89 | + Set<Integer> set1 = new HashSet(); |
| 90 | + for(int i = 0; i < nums1.length; i++){ |
| 91 | + set1.add(nums1[i]); |
| 92 | + } |
| 93 | + Set<Integer> intersect = new HashSet(); |
| 94 | + for(int i = 0; i < nums2.length; i++){ |
| 95 | + if(set1.contains(nums2[i])){ |
| 96 | + intersect.add(nums2[i]); |
| 97 | + } |
| 98 | + } |
| 99 | + int[] result = new int[intersect.size()]; |
| 100 | + Iterator<Integer> it = intersect.iterator(); |
| 101 | + for(int i = 0; i < intersect.size(); i++){ |
| 102 | + result[i] = it.next(); |
| 103 | + } |
| 104 | + return result; |
| 105 | + } |
23 | 106 |
|
24 | 107 | //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.
|
25 | 108 | public int[] intersection_naive(int[] nums1, int[] nums2) {
|
|
0 commit comments