704. Binary Search

202407040947
tags: #binary-search

var search = function(nums, target) {
	let start = 0;
	let end = nums.length - 1;
	while (start <= end) {
		const mid = Math.floor((end - start) / 2) + start;
		if (nums[mid] === target) {
			return mid;
		}
		if (nums[mid] < target) {
			start = mid + 1;
		} else {
			end = mid - 1;
		}
	}
	return -1;
};

Reference

https://leetcode.com/problems/binary-search/