class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @param target int整型
* @return int整型
*/
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while(left<right){
int mid = left + (right-left)/2;
if(target > nums[mid]){
left = mid + 1;
} else if(target < nums[mid]){
right = mid;
} else{
return mid;
}
}
return -1;
// write code here
}
};左闭右开
https://www.programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#%E6%80%9D%E8%B7%AF