LeetCode33. Search in Rotated Sorted Array

1、题目链接

https://leetcode.com/problems/search-in-rotated-sorted-array/

2、解题思路

刚开始看这道题的时候,没注意看最后的要求,感觉很傻逼的一道题,直接遍历数组,然后判断目标值是否和数组中的值相等,然后返回下标,不存在就返回-1,后面才发现题目要求在时间复杂度在O(log n)级别的,而且题目是说给你一个排序的数组,但是这个排序的数组发生了一次意外,被旋转了一次,由此我们想到了二分查找算法,但是这是一个变种的二分查找,需要根据实际情况来分多种情况考虑,由于只发生了一次旋转(按照数组中的某个值进行旋转,也就是换位置),我们可以认定当我们把数组一分为二的时候至少有一半是有序的,另一半可能有序,详细看我在下列代码中的分析。

3、代码

  • Java:
public static int search(int[] nums, int target) {
    if (null == nums || nums.length == 0 || (nums.length == 1 && target != nums[0])) {
        return -1;
    }
    if (nums.length == 1) {
        return 0;
    }
    int left = 0;
    int right = nums.length - 1;
    while (left < right) {
        int middle = (left + right) / 2;
        if (nums[left] == target) {
            return left;
        }
        if (nums[middle] == target) {
            return middle;
        }
        if (nums[right] == target) {
            return right;
        }
        if (nums[left] < nums[middle]) {  //左边是有序的
            if (nums[middle] > target && nums[left] < target) {
                //目标值在有序排列的左边
                right = middle - 1;
            } else {
                //目标值在无序排列的右边
                left = middle + 1;
            }
        } else {  //右边是有序的
            //nums[middle] < nums[left]
            if (nums[middle] < target && nums[right] > target) {
                //目标值在有序排列的右边
                left = middle + 1;
            } else {
                //目标值在无序排列的左边
                right = middle - 1;
            }
        }
    }
    return -1;
}
  • Python
def search(self, nums, target):
    if nums is None or len(nums) == 0 or (len(nums) == 1 and nums[0] != target):
        return -1
    if len(nums) == 1 and nums[0] == target:
        return 0
    left = 0
    right = len(nums) - 1
    while left < right:
        middle = (left + right) / 2
        if nums[left] == target:
            return left
        if nums[middle] == target:
            return middle
        if nums[right] == target:
            return right
        if nums[left] < nums[middle]:
            if nums[middle] > target > nums[left]:
                right = middle - 1
            else:
                left = middle + 1
        else:
            if nums[middle] < target < nums[right]:
                left = middle + 1
            else:
                right = middle - 1
    return -1
  • JavaScript
var search = function (nums, target) {
    if (undefined == nums || null == nums
        || nums.length == 0 || (nums.length == 1 && nums[0] != target)) {
        return -1
    }
    if (nums.length == 1 && nums[0] == target) {
        return 0
    }
    let left = 0
    let right = nums.length - 1

    while (left < right) {
        let middle = parseInt((left + right) / 2)
        if (nums[left] == target) {
            return left
        } else if (nums[middle] == target) {
            return middle
        } else if (nums[right] == target) {
            return right
        }
        if (nums[left] < nums[middle]) {
            if (nums[middle] > target && nums[left] < target) {
                right = middle - 1
            } else {
                left = middle + 1
            }
        } else {
            if (nums[middle] < target && target < nums[right]) {
                left = middle + 1
            } else {
                right = middle - 1
            }
        }
    }
    return -1
};

4、提交结果

ETBHIJ.md.png

你可能感兴趣的:(LeetCode33. Search in Rotated Sorted Array)