//定义在左闭右闭区间上的二分查找classSolution{public:intsearch(vector<int>& nums,int target){int n = nums.size();int left =0, right = n -1;while(left <= right){int middle = left +((right - left)>>1);if(nums[middle]> target){
right = middle -1;}elseif(nums[middle]< target){
left = middle +1;}else{return middle;}}return-1;}};
//定义在左闭右开区间上的二分查找classSolution{public:intsearch(vector<int>& nums,int target){int n = nums.size();//查找区间的初始定义为左闭右开区间,就要在循环中坚持在左闭右开区间上查找int left =0, right = n;while(left < right){int middle = left +((right - left)>>1);if(nums[middle]> target){
right = middle;}elseif(nums[middle]< target){
left = middle +1;}else{return middle;}}return-1;}};