Javascript beats 97% 2 pointer


  • 0
    R
    var intersect = function(nums1, nums2) {
        nums1.sort(cm);
        nums2.sort(cm);
        
        var p1 =0
        var p2 =0
        
        var res = [];
        
        while(p1 < nums1.length && p2 < nums2.length) {
            if(nums1[p1] == nums2[p2]) {
                res.push(nums1[p1]);
                p1++;
                p2++;
            } else if(nums1[p1] > nums2[p2]) {
                p2++;
            } else {
                p1++;
            }
        }
        return res;
    };
    
    var cm = function(a, b) {
        return a - b;
    }
    

Log in to reply
 

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