Open mengjoy opened 3 years ago
二分法适合有序的数组
var search = function(nums, target) { let left = 0, right = nums.length - 1, tmp; while(left <= right) { tmp = Math.floor((left + right) / 2) if(nums[tmp] === target) { return tmp; } // 相当于已经比较完mid,就不再使用这个值 if(nums[tmp] < target) { left = tmp+ 1 } else { right = tmp - 1 } } return -1 };
二分法适合有序的数组