JavaScript beat 100%


  • 0
    A
    var threeSumSmaller = function(nums, target) {
        var count = 0, i, lo, hi, len=nums.length;
        
        if(len<3) return count;
    
        nums.sort(function(a,b){return a-b;});
    
        for(i=1;i<len-1;i++){
            lo = 0;
            hi = len-1;
            
            while(lo < i && i < hi){
                if(nums[lo]+nums[i]+nums[hi]>=target){
                    hi--;
                } else {
                    count += hi-i;
                    lo++;
                }
            }
        }
        
        return count;
    };
    

Log in to reply
 

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