[LeetCode 题解] Search in Rotated Sorted Array

前言

【LeetCode 题解】系列传送门:
http://www.cnblogs.com/double-win/category/573499.htmlhtml

题目描述

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

(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.code

You may assume no duplicate exists in the array.htm

题意

假设有一个有序的循环数组, 其起始位置未知。
如 序列0 1 2 4 5 6 7 可能的数组形式为 4 5 6 7 0 1 2blog

给定一个目标数据,查找该数据是否在给定的数组中出现。 若是在数组中找到该目标数据,则返回该目标数据所在的数组下标, 不然返回-1.ip

Tips: 假设给定的数组中没有重复的元素get

思路

根据题意提取关键信息: 在一个sorted array中查找给定元素
首先可以想到的是binary search。 因为数组是一个循环数组, 那么数组的最小值和最大值并不必定分别在数组两端。
(1) 思路一: 从头至尾,依次遍历数组, 查找目标元素, 其时间复杂度O(n)
(2) 思路二: 从头至尾寻找min, max, 而后将左侧较大的数据concat到右侧, 而后再用binary search。 时间复杂度 O(n/2) + O(logn) ~ O(n)
(3) 思路三:若是将原始数组分割成左右两半, 能够发现其具备以下几种情形:博客

case 1: 最小值和最大值分别在左右两侧:
leftpart: [0 1 2 4] rightpart:[5 6 7]
值得注意的是该状况最大值只能在最右侧, 且最小值只能在最左侧it

case 2:
最大值和最小值同侧: 最大值在右侧
leftpart: [ 2 4 5 6] rightpart: [7 0 1]
能够发现 leftValue = 2 medianValue = 6 rightValue = 1
medianValue > leftValue && medianValue > rightValue:
若是 target > medianValue 或者 target < rightValue, 那么必在rightpart
不然,必在leftpart

case 3:
最大值和最小值同侧: 最小值在左侧
leftpart: [6 7 0 1] rightpart: [2 4 5]
能够发现 leftValue = 6 medianValue = 1 rightValue = 5
medianValue < leftValue && medianValue < rightValue:
若是 target < medianValue 或者 target > rightValue, 那么必在leftpart
不然,必在rightpart

加上一些边界条件,即得结果。

解法

class Solution(object):
    def findTarget(self, nums, minIndex, maxIndex, target):
        """
        :type nums: List[index]
        :type minIndex: int 
        :type maxIndex: int
        :type target: int
        :rtype: int
        """
        if nums[minIndex] == target:
            return minIndex
        if nums[maxIndex] == target:
            return maxIndex

        if maxIndex == minIndex:
            return 0  if target == nums[minIndex] else -1

        median = (minIndex + maxIndex) / 2
        if nums[median] == target:
            return median

        if nums[median] > nums[minIndex] and nums[median] > nums[maxIndex]:
            # maxValue and minValue is in right part
            if target > nums[median] or target < nums[minIndex]:
                return self.findTarget(nums, median + 1, maxIndex, target)
            else:
                return self.findTarget(nums, minIndex, median, target)
        elif nums[median] < nums[minIndex] and nums[median] < nums[maxIndex]:
            # maxValue is in left part
            if target < nums[median] or target > nums[maxIndex]:
                return self.findTarget(nums, minIndex, median, target)
            else:
                return self.findTarget(nums, median + 1, maxIndex, target)
        else:
            # maxValue is in maxIndex and minValue is in minIndex
            if target < nums[minIndex] or target > nums[maxIndex]:
                return -1
            elif target > nums[median]:
                return self.findTarget(nums, median + 1, maxIndex, target)
            else:
                return self.findTarget(nums, minIndex, median, target)

        
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        numSize = len(nums)
        # the array is empty
        if numSize == 0:
            return -1
        minIndex = 0
        maxIndex = numSize - 1
        return self.findTarget(nums, minIndex, maxIndex, target)

声明

做者 Double_Win
出处 http://www.cnblogs.com/double-win/p/7966913.html
本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文连接,不然做者保留追究法律责任的权利。 若本文对你有所帮助,您的关注和推荐是咱们分享知识的动力!
相关文章
相关标签/搜索