Created
January 26, 2023 11:35
-
-
Save AdilsonFuxe/0ec30df9cff40858ab79427fb55f9ada to your computer and use it in GitHub Desktop.
704. Binary Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number} | |
*/ | |
var search = function(nums, target) { | |
let start = 0; | |
let end = nums.length -1; | |
while(start <= end) { | |
let middle = Math.trunc((start + end) / 2); | |
if(nums[middle] == target) | |
return middle; | |
else if (nums[middle] < target) | |
start = middle + 1; | |
else | |
end = middle - 1; | |
} | |
return -1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment