class Solution {
public:
int search(vector<int>& nums, int target) {
int length = nums.size();//计算容器长度
int left = 0;//0
int right = length-1;//5
int middle = 0;
/*
while(left<=right){
middle = (left+right)/2;
//middle = (left+right)>>1;
if(nums[middle]>target)
right = middle-1;
else if(nums[middle]<target)
left = middle+1;
else
return middle;
}*/
//另一种写法
while(left<=right){
middle = (left+right)>>1;
if(nums[middle]!=target)
nums[middle]>target?(right = middle-1):(left = middle+1);
else
return middle;
}
return -1;
}
};