白菜刷LeetCode记-704. Binary Search
简单题,直接是用来复习二分查找的。题目如下:
代码如下:
/** * @param {number[]} nums * @param {number} target * @return {number} */ var search = function(nums, target) { let len = nums.length; let lp = 0, rp = len -1; let mid = parseInt((lp + rp)/2); while(lp <= rp){ if(target == nums[mid]){ return mid; }else if(target < nums[mid]){ rp = mid - 1; }else{ lp = mid + 1; } mid = parseInt((lp+rp)/2); } return -1; }