class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 2;
while(left <= right){
int mid = left + (right - left) / 2;
if(isblue(nums,target,mid)){
right = mid - 1;
}else{
left = mid + 1;
}
}
if(left == nums.length || nums[left] != target){
return -1;
}
return left;
}
public boolean isblue(int[] nums, int target, int i) {
if(nums[i] > nums[nums.length-1]){
return target > nums[nums.length-1] && target <= nums[i];
}else{
return target > nums[nums.length-1] || nums[i] >= target;
}
}
}
搜索排序数组,如果用while(left<=right)写法,返回的是left
最新推荐文章于 2024-07-08 22:12:26 发布