题解 | #17.二分查找-I#
二分查找-I
http://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
注意:
left<=right
function search( nums , target ) {
let left = 0, right = nums.length-1;
while(left <= right){
let mid = parseInt( (left + right)/2 );
if(nums[mid] < target){//target在右边继续查找
left = mid+1;
}else if(nums[mid] > target){//target在左边继续查找
right = mid-1;
}else{
return mid;
}
}
return -1;
}
module.exports = {
search : search
};