33 & 81. Search in Rotated Sorted Array I & II

33. Search in Rotated Sorted Arrayarrow-up-right

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

My Solutions:

方法1:在整个nums的范围里binary search。每一次根据反转的index在mid之前还是之后,以及target的位置,调整l和r的位置。

class Solution {
    public int search(int[] nums, int target) {

    if (nums == null || nums.length == 0) return -1;
    int l = 0, r = nums.length - 1;
    while (l + 1 < r) {
        int mid = l + (r - l) / 2;
        if (nums[mid] == target) return mid;
        
        if (nums[mid] < nums[r]) { // 反转的index在mid之前,mid之后的都是递增的
            // target在mid和r之间
            if (target > nums[mid] && target <= nums[r]) l = mid;
            else r = mid;

        } else { // 反转的index在mid之后,mid之前的都是递增的
            //target在mid和l之间
            if (target >= nums[l] && target < nums[mid]) r = mid;
            else l = mid;
        }
    }
    
    if (nums[l] == target) return l;
    else if (nums[r] == target) return r;
    else return -1;
}

方法2:先找到转折的index,检查target在转折之前还是之后,然后在那半段里binary search。

81. Search in Rotated Sorted Array IIarrow-up-right

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

Example 2:

Follow up:

My Solutions:

和33类似,因为有duplicate number的关系,需要把duplicate的去掉。

Last updated