题目:
代码:
class Solution {
public:
int search(vector<int>& nums, int target) {
int begin=0,end=nums.size()-1;
int mid;
while(begin<=end){
mid=begin+(end-begin)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) end=mid-1;
else begin=mid+1;
}
return -1;
}
};
注意begin<end和begin<=end这两种情况的区别即可