1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include<iostearm> using namespace std; int main() { vector<int> nums(n); int target; int left = 0 , right = n -1; while(left < right) { int mid = (left + mid)/2; if (nums[mid] == target) return mid; if (nums[mid] > target) right = mid; else left = mid + 1; } while(left < right) { int mid = (left + mid + 1)/2; if (nums[mid] == target) return mid; if (nums[mid] > target) right = mid - 1; else left = mid; } }
|