严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Intersection Of Two ArraysI WayII

Go down

Intersection Of Two ArraysI WayII Empty Intersection Of Two ArraysI WayII

Post by Admin Sat Oct 21, 2017 2:19 pm

public int[] intersection(int[] nums1, int[] nums2) {
   Arrays.sort(nums1);
   Arrays.sort(nums2);
 
   ArrayList<Integer> list = new ArrayList<Integer>();
   for(int i=0; i<nums1.length; i++){
       if(i==0 || (i>0 && nums1[i]!=nums1[i-1])){
           if(Arrays.binarySearch(nums2, nums1[i])>-1){
               list.add(nums1[i]);
           }
       }
   }
 
   int[] result = new int[list.size()];
   int k=0;
   for(int i: list){
       result[k++] = i;
   }
 
   return result;
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum