0704.cpp (399B)
1 class Solution { 2 public: 3 int search(vector<int> &nums, int target) { 4 int low = 0, high = nums.size() - 1; 5 while (low <= high) { 6 int mid = low + (high - low) / 2; 7 if (nums[mid] == target) return mid; 8 if (nums[mid] < target) 9 low = mid + 1; 10 else 11 high = mid - 1; 12 } 13 return -1; 14 } 15 };