Java solution simple


  • 0
    A
    class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            Arrays.sort(nums1);
            Arrays.sort(nums2);
            if(nums1.length>nums2.length)
                return intersection(nums1,nums2);
            else
                return intersection(nums2,nums1);
        }
        public int[] intersection(int[] a, int[] b)
        {   int temp[]=new int[a.length];
            int k=0;
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b.length;j++){
                    if(a[i]==b[j])
                        temp[k]=a[i];
                        k++;
                }
            }
         return temp;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.