二分查找相关题目-旋转数组查找_leetcode


二分查找相关题目-旋转数组查找_leetcode_02


二分查找相关题目-旋转数组查找_leetcode_03


二分查找相关题目-旋转数组查找_leetcode_04


二分查找相关题目-旋转数组查找_leetcode_05


二分查找相关题目-旋转数组查找_leetcode_06


二分查找相关题目-旋转数组查找_leetcode_07

class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + (right - left)/2;
if (nums[mid] == target) return mid;
if (nums[mid] < nums[right]){
//右边有序
if (nums[mid] < target && target <= nums[right]) left = mid + 1;else right = mid - 1;
}else{
//左边有序
if (nums[left] <= target && target < nums[mid]) right = mid - 1;else left = mid + 1;
}
}

return -1;
}
};