题目描述
思路
- 如果nums[left] <= nums[mid],则[left, mid]有序
- 如果nums[left] > nums[mid],则[mid,right]有序
方法一:
class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) return -1;
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (target == nums[mid]) return mid;
if (nums[left] <= nums[mid]) { // 左边有序
if (target >= nums[left] && target < nums[mid]) { // target在左边
right = mid - 1;
} else { // target在右边
left = mid + 1;
}
} else { // 右边有序
if (target > nums[mid] && target <= nums[right]) { // target在右边
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}
标签:right,target,nums,int,mid,Hot,100,LeetCode33,left
From: https://www.cnblogs.com/keyongkang/p/17914661.html