var nums=[1,2,3,4];
var target=3;
// 左闭右闭
var find=(nums,target)=>{
var l=0;
var r=nums.length-1;
while(l<=r){var mid=Math.floor((l+r)/2);
if(nums[mid]>target){r=mid-1;}
else if(nums[mid]<target){l=mid+1;}else{return nums[mid];
}
}
return -1;
}
// 左闭右开
var find2=(nums,target)=>{
var l=0;
var r=nums.length;
while(l<r){var mid=Math.floor((l+r)/2);
if(nums[mid]>target){r=mid;}else if(nums[mid]<target){l=mid+1;}else{return nums[mid];
}
}
return -1;
}
// 左闭右开,递归写法
var find3=(nums,t)=>{var search=(l,r)=>{if(l>=r) return -1
var mid=Math.floor((l+r)/2)
if(nums[mid]>target){return search(l,mid)
}else if(nums[mid]<target){return search(mid+1,r)
}else{return mid}
}
return search(0,nums.length)
}