33. Search in Rotated Sorted Array

Medium
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

Solution:

class Solution {
    public int search(int[] nums, int target) {
        return search(nums,0,nums.length-1,target);
    }
    //test case : 1,3
    when nums[mid] >= nums[end] && nums[mid] >= nums[start]
    there are only element, mid is end or start, it should be able to search
    
    public int search(int[] nums, int start, int end, int target){
        if(start > end) return -1;
        int mid = (start+end)/2;
        if(target == nums[mid]) return mid;
        if(nums[mid] <= nums[end] && nums[start] <= nums[mid]){
            if(target > nums[mid]){
                return search(nums,mid+1, end, target);
            }else{
                return search(nums,start, mid-1, target);
            }
        }else if(nums[mid] >= nums[end] && nums[mid] >= nums[start]){
            if(target < nums[mid] && target >= nums[start]){
                return search(nums,start, mid-1, target);
            }else{
                return search(nums,mid+1, end, target);
            }
        }else if(nums[mid] <= nums[end] && nums[mid] <= nums[start]){
            if(target > nums[mid] && target <= nums[end]){
                return search(nums,mid+1, end, target);
            }else{
                return search(nums,start, mid-1, target);
            }
        }
        return -1;
    }
}
class Solution {
    public int search(int[] nums, int target) {
        return search(nums,0,nums.length-1,target);
    }
    //combine case1 with case2 with condition nums[mid] >= nums[start]
    test case 1,3 mid == start
    when nums[mid] >= nums[start]
    there are only one element, mid is start, it should be able to search
    
    public int search(int[] nums, int start, int end, int target){
        if(start > end) return -1;
        int mid = (start+end)/2;
        if(target == nums[mid]) return mid;
        if(nums[mid] >= nums[start]){
            if(target < nums[mid] && target >= nums[start]){
                return search(nums,start, mid-1, target);
            }else{
                return search(nums,mid+1, end, target);
            }
        }else{
            if(target > nums[mid] && target <= nums[end]){
                return search(nums,mid+1, end, target);
            }else{
                return search(nums,start, mid-1, target);
            }
        }
        return -1;
    }
}

Comments

Popular posts from this blog

MockInterview:785. Is Graph Bipartite?

geeksforgeeks:findFind zeroes to be flipped so that number of consecutive 1’s is maximized

94.Binary Tree Inorder Traversal