二分查找

int binarySearch(vector<int>& nums, int target) {
    int left = 0;
    int right = nums.size() - 1;
    int mid = nums.size() / 2;
    while (left <= right && nums[mid] != target) {
        if (nums[mid] > target) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
        mid = left + (right - left) / 2;
    }
    if (left > right) return -1;
    return mid;
}