class Solution {
public:
int search(vector<int>& nums, int target) {
int left=0;
int right=nums.size()-1;
int middle=(right-left)/2;
while(right>=left){
if(target>nums[middle]){
left=middle+1;
middle=(left+right)/2;
}
else if(target<nums[middle]){
right=middle-1;
middle=(right-left)/2;
}
else{
return middle;
}
}
return -1;
}
};
力扣704.二分查找
最新推荐文章于 2024-09-09 00:44:52 发布