JS solution beats 95%


  • 0
    L
    var search = function(nums, target) {
        if(!nums.length)
        return false;
        var head=0;
        var tail=nums.length-1;
        if(nums[head]<=target){
            while(head<nums.length&&nums[head]!==target){
                head++;
            }
            return (nums[head]===target)? true:false;
        }
        if(nums[tail]>=target){
            while(tail>=0&&nums[tail]!==target){
                tail--;
            }
            return (nums[tail]===target)? true:false;
        }
        if(nums[tail]<target&&nums[head]>target)
        return false;
    };
    

Log in to reply
 

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