基本二分查找-LeetCode

在这里插入图片描述

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int count = nums.size() - 1;
        int low = 0;
        int high = count;
        int mid = 0;
        while(low <= high){
            mid = low + (high-low)/2;
            if(nums[mid]==target){
                return mid;
            }
            else if(nums[mid] < target){
                low = mid + 1;
            }
            else if(nums[mid] > target){
                high = mid - 1;
            }
        }
        return -1;
    }
};


posted @ 2024-10-31 16:16  在天边偷看小天使  阅读(7)  评论(0)    收藏  举报  来源