好像写过?算了再写一遍
class Solution {
public:
int search(vector<int>& nums, int target) {
if (nums.empty()) {
return -1;
}
auto beg = nums.begin(), end = nums.end();
auto mid = beg + (end - beg) / 2;
while (beg <= end) {
if (*mid == target) {
return mid - nums.begin();
}
if (*mid > target) {
end = mid - 1;
}
if (*mid < target) {
beg = mid + 1;
}
mid = beg + (end - beg) / 2;
}
return -1;
}
};