/*** 二分查找,也成为折半查找,每次都能将查找区间减半,时间复杂度为 O(logN)** 中间值查找方式 Math.floor((l + h) / 2)** 示例* Input : [1,2,3,4,5]* key : 3* return the index : 2**/function binarySearch(nums, target) {let l = 0,h = nums.length - 1;while (l <= h) {let m = Math.floor((l + h) / 2);if (nums[m] > target) {h = m ;} else if (nums[m] < target) {l = m + 1;} else {return m}}}
