LeetCode 33. 搜索旋转排序数组 Search in Rotated Sorted Array

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。

你可以假设数组中不存在重复的元素。

你的算法时间复杂度必须是 O(log n) 级别。

示例 1:

输入: nums = [4,5,6,7,0,1,2], target = 0
输出: 4

示例 2:

输入: nums = [4,5,6,7,0,1,2],target = 3
输出: -1
public class Solution {
    public int search(int[] nums, int target) {
        int len = nums.length;
        if (len == 0) return -1;
        return binarySearch(nums, 0, len-1, target);
    }
    public int binarySearch(int[] nums, int left, int right, int target) {
        if (left > right) return -1;
        
        int mid = (left + right) / 2;
        
        if (nums[left] == target){
            return left;
        } 
        
        if (nums[mid] == target){
            return mid;
        } 
        
        if (nums[right] == target){
            return right;
        } 
        
        if (nums[left] < nums[right]) { 
            if (target < nums[left] || target > nums[right]) {    
                return -1;
            } else if (target < nums[mid]) {                   
                return binarySearch(nums, left+1, mid-1, target);
            } else {                                        
                return binarySearch(nums, mid+1, right-1, target);
            }
        }else if (nums[left] < nums[mid]) { 
            if (target > nums[left] && target < nums[mid]) {     
                return binarySearch(nums, left+1, mid-1, target);
            } else {                                        
                return binarySearch(nums, mid+1, right-1, target);
            }
        } else { 
            if (target > nums[mid] && target < nums[right]) {     
                return binarySearch(nums, mid+1, right-1, target);
            } else{                                         
                return binarySearch(nums, left+1, mid-1, target);
            }
        }
    }
}

LeetCode 33. 搜索旋转排序数组 Search in Rotated Sorted Array_第1张图片 

 

你可能感兴趣的:(LeetCode,算法,LeetCode,搜索旋转排序数组,Search,in,Rotated,Sorted,Array)