6ms easy java solution


  • 0
    Z
            int start=0, end = nums.length-1, pos=-1, mid;
            
            while(start<=end)
            {
                mid = start + (end-start)/2;
                if(nums[mid] == target)
                    return mid;
                else if(nums[start]<target && end-start==0)
                    return start+1;
                else if(nums[start]>target && end-start==0)
                    return start;
                else if(nums[mid] > target)
                    end = mid-1<0 ? 0:end-1;
                else if (nums[mid] < target)
                    start = mid+1;
                    
            }  
            return -1;
        }```

Log in to reply
 

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