天天看点

LintCode领扣 题解丨微软高频题:搜索旋转排序数组

假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。你可以假设数组中不存在重复的元素。

在线评测地址:

https://www.lintcode.com/problem/search-in-rotated-sorted-array/?utm_source=sc-tianchi-sz0827

例1:

输入: [4, 5, 1, 2, 3] and target=1,

输出: 2.

例2:

输入: [4, 5, 1, 2, 3] and target=0,

输出: -1.

【题解】

算法:二分

根据题目我们可以知道旋转数组实际上是两个递增数组的组成,且第一个数组中的最小值大于第二个数组的最大值

由于数组中不存在重复的元素,那么我们可以先找到target在哪个数组,再进行二分

代码思路

二分找到第二个数组的起始位置,即整个数组的最小值的位置minPosition

通过比较target和第二个数组最小元素(即最后一个数)大小关系判断target在哪一个数组

对target所在的数组二分

复杂度分析

N表示为 A 数组的长度

空 间复杂度:O(N)

时间复杂度:O(logN)

public class Solution {

/**
 * @param A: an integer rotated sorted array
 * @param target: an integer to be searched
 * @return: an integer
 */
public int search(int[] A, int target) {
    if (A == null || A.length == 0) { 
        return -1; 
    } 
        
    //找到数组最小值位置minPosition,即第二个数组的起始位置
    int minPosition = 0; 
    intleft = 0; 
    int right = A.length - 1; 
    while (left + 1 < right) { 
        int mid = left + (right - left) / 2; 
        if (A[mid] > A[right]) { 
           left = mid; 
        } else { 
            right = mid; 
        } 
    }         
         
    if (A[left] < A[right]) { 
        minPosition = left; 
    } else { 
        minPosition = right; 
    } 
         
    //判断target在哪一个数组中
    if (A[A.length - 1] < target) { 
        left = 0; 
        right = minPosition - 1; 
    } else { 
        left = minPosition; 
        right = A.length - 1; 
    }
         
    //对target所在数组二分搜索
    while (left + 1 < right) { 
        int mid = left + (right - left) / 2; 
        if (A[mid] < target) { 
            left = mid; 
        } else { 
            right = mid; 
        } 
    }          
        
    if (A[left] == target) { 
        return left; 
    } 
    if (A[right] == target) { 
        return right; 
    }         
    return -1; 
}           

}

更多题解参见九章算法官网:

https://www.jiuzhang.com/solution/search-in-rotated-sorted-array/?utm_source=sc-tianchi-sz0827

继续阅读