Intersection of Two Arrays

2/22/2018 update
这道题还是挺有意思的,用于拓宽思路。
Binary search solution:

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums2 == null
            || nums1.length == 0 || nums2.length == 0) return new int[]{};
        Set<Integer> set = new HashSet<>();
        Arrays.sort(nums2);
        for (int n: nums1) {
            if (search(nums2, n)) {
                set.add(n);
            }
        }
        int[] res = new int[set.size()];
        int index = 0;
        for (int n: set) {
            res[index++] = n;
        }
        return res;
    }

    private boolean search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l + 1 < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] <= target) {
                l = mid;
            } else {
                r = mid;
            }
        }

        return (nums[l] == target || nums[r] == target) ? true : false;
    }
}

Two pointers solution:
// two pointers
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums2 == null) return null;
        if (nums1.length == 0|| nums2.length == 0) return new int[]{};
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        Set<Integer> set = new HashSet<>();
        int i = 0, j = 0;
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] == nums2[j]) {
                set.add(nums1[i]);
                i++;
                j++;
            } else  if(nums1[i] < nums2[j]) {
                i++;
            } else {
                j++;
            }
        }

        int[] res = new int[set.size()];
        int index = 0;
        for (int n: set) {
            res[index++] = n;
        }
        return res;
    }
}

思路:
三种解法
1. two hashsets
......
2. sort & binary search
也可以用hashset来保存unique number。做了这道题才知道hashset只能保存unique值。
要注意检查nums1.length == 0的情况,在二分法最后double check的时候可能会越界。
代码如下:

 public class Solution {  
   /*  
    * @param nums1: an integer array  
    * @param nums2: an integer array  
    * @return: an integer array  
    */  
   public int[] intersection(int[] nums1, int[] nums2) {  
     // write your code here  
     if(nums1 == null || nums2 == null )  
      return null;  
      if(nums1.length == 0|| nums2.length == 0)  
       return new int[]{};  
     Arrays.sort(nums1);  
     Arrays.sort(nums2);  
     List<Integer> interList = new ArrayList<>();  
     for(int i = 0; i < nums2.length; i++) {  
       if(i > 0)  
       {  
       if(nums2[i] == nums2[i-1])   
         continue;  
       }  
       if(search(nums1, nums2[i])) {  
         interList.add(nums2[i]);  
       }  
     }  
     int[] res = new int[interList.size()];  
     int i = 0;  
     for(int n: interList) {  
       res[i++] = n;  
     }  
     return res;  
   }  
   private boolean search(int[] nums, int target) {  
     int start = 0;  
     int end = nums.length-1;  
     while(start + 1 < end) {  
       int mid = start + (end-start)/2;  
       if(nums[mid] == target) {  
         return true;  
       }   
       if( nums[mid] < target) {  
         start = mid;  
       } else  
        {  
          end = mid;  
        }  
     }  
     if(nums[start] == target)   
       return true;  
     if(nums[end] == target)  
       return true;  
     return false;  
   }  
 };  

3. sort & merge
先排序,然后用merge的思想对比2个数组里面的元素,如果小就跳过,相等就加到set里面。set自动去重复。
代码如下:


 public class Solution {  
   /*  
    * @param nums1: an integer array  
    * @param nums2: an integer array  
    * @return: an integer array  
    */  
   public int[] intersection(int[] nums1, int[] nums2) {  
     // write your code here  
     if(nums1 == null || nums2 == null )  
      return null;  
      if(nums1.length == 0|| nums2.length == 0)  
       return new int[]{};  
     Arrays.sort(nums1);  
     Arrays.sort(nums2);  
     Set<Integer> interSet = new HashSet<>();  
     int i = 0;   
     int j = 0;  
     while(i < nums1.length && j < nums2.length) {  
       if(nums1[i] < nums2[j]) {  
         i++;  
       } else if(nums1[i] > nums2[j]) {  
         j++;  
       } else {  
         j++;  
         interSet.add(nums1[i++]);  
       }  
     }  
     int[] result = new int[interSet.size()];  
     int k = 0;  
     for (Integer num : interSet) {  
       result[k++] = num;  
     }  
     return result;  
   }  
 };  

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array