js binary search


  • 0
    3
    var intersection  = (nums1,nums2)=>{
        nums1.sort((a,b)=>{return a-b;});
        nums2.sort((a,b)=>{return a-b;});
        var i = 0, j = 0, ret = [];
        while(j<nums2.length&&i<nums1.length){
            if(nums1[i]==nums2[j]){
                if(ret[ret.length-1]!=nums1[i]){
                    ret.push(nums1[i]);
                }
                i++;
                j++;
            }
            else if(nums1[i]<nums2[j]){
                i++;
            }
            else {
                j++;
            }
        }
        return ret;
    }
    

Log in to reply
 

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