Skip to content

Instantly share code, notes, and snippets.

@tamarous
Created September 9, 2018 07:14
Show Gist options
  • Save tamarous/e65807907a254dad492d53431b89fd8a to your computer and use it in GitHub Desktop.
Save tamarous/e65807907a254dad492d53431b89fd8a to your computer and use it in GitHub Desktop.
在旋转数组中寻找一个数字
class Solution {
public:
int search(vector<int>& nums, int target) {
int size = nums.size();
int low = 0, high = size;
while(low < high) {
int mid = low + (high-low)/2;
if (nums[mid] == target) {
return mid;
}
if (nums[low] <= nums[mid]) {
if (target >= nums[low] && target < nums[mid]) {
high = mid;
} else {
low = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[high-1]) {
low = mid + 1;
} else {
high = mid;
}
}
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment