日撸leetCode三道题---Day1---二分查找

二分查找时间复杂度为O(log(n))
针对有序数组

//定义查找区间
var low = 0
var high = n
//循环查找
while(low<high)//这里不能等于,不然会越界
{
	var mid = Math.floor(low+(high-low)/2)
	if(target>nums[mid]){
           low = mid+1
     }else if(target ==nums[mid]){
           return mid
     }else if(target<nums[mid]){
           high=mid
     }
     return low  //这是区间端口会相等,直接返回
}

菜鸟学算法第一天小结

你可能感兴趣的:(算法,js,leetcode,算法,javascript)