二分模板

int search(vector<int>& nums, int target) {
    int left = 0, right = nums.size();
    int mid;
    while(left < right) {
        mid = (left + right) >> 1;
        if(nums[mid] == target) return mid;
        else if(nums[mid] < target) left = mid + 1;
        else right = mid;
    }
    if(nums[mid] != target) return -1;
    return mid;
}
posted @ 2022-09-23 15:01  Zforw  阅读(14)  评论(0编辑  收藏  举报