leetcode刷题(python)--33. Search in Rotated Sorted Array(思路以及两种解法-迭代和递归)

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).

思路:

需要使用二分法的变体:

这里的数组是首先从小到大排序,然后经过绕着某个轴旋转,比如[0,1,2,4,5,6,7]旋转后得到[4,5,6,7,0,1,2]

那么没法直接使用二分法,我们这里注意到数组之前是从小到大排序的,因为需要输出目标值的索引,那么我们不能去修改那么使用三个flag: left, mid, right

来记录每一次需要搜索的左边界,中间点和右边界:

首先确定边界,不管是递归还是迭代,必须保证left <= right, 在这个前提下,

因为没有重复项,每次搜索前先查看nums[left], nums[mid], nums[right],

若与目标值相等:直接返回

若不相等:

        1.比较nums[mid]和nums[right]:

                如果nums[mid] < nums[right],说明(mid, right)是有序的:

                            如果target属于区间(mid, right),就在这个区间接着搜索,重复之前步骤;

                            如果target不属于区间(mid, right),就在(left, mid)接着搜索,重复之前步骤;

                如果nums[mid] > nums[right],

                如果nums[mid] < nums[right],说明(left, mid)是有序的:

                            如果target属于区间(left, mid),就在这个区间接着搜索,重复之前步骤;

                            如果target不属于区间(left, mid),就在(mid, right)接着搜索,重复之前步骤;

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
[python]  view plain  copy
  1. class Solution:  
  2.     def search(self, nums, target):  
  3.         """ 
  4.         :type nums: List[int] 
  5.         :type target: int 
  6.         :rtype: int 
  7.         """  
  8.         l = 0   
  9.         r = len(nums)-1  
  10.         return self.bin_search(l,r,nums,target)  
  11.     def bin_search(self,l, r, nums, target):  
  12.         m = (l+r)//2  
  13.         if l > r:  
  14.             return -1  
  15.         if target == nums[m]:  
  16.             return m  
  17.         elif target == nums[r]:  
  18.             return r  
  19.         elif target == nums[l]:  
  20.             return l  
  21.         else:  
  22.             if nums[m]
  23.                 if target > nums[m] and target < nums[r]:  
  24.                     return self.bin_search(m + 1, r, nums, target)  
  25.                 else:  
  26.                     return self.bin_search(l, m-1, nums, target)  
  27.             else:  
  28.                 if target < nums[m] and target > nums[l]:  
  29.                     return self.bin_search(l, m-1, nums, target)  
  30.                 else:  
  31.                     return self.bin_search(m+1, r , nums, target)  
[python]  view plain  copy
  1.   
[python]  view plain  copy
  1. class Solution:  
  2.     def search(self, nums, target):  
  3.         """ 
  4.         :type nums: List[int] 
  5.         :type target: int 
  6.         :rtype: int 
  7.         """  
  8.         l = 0   
  9.         r = len(nums)-1  
  10.         if len(nums) == 0:  
  11.             return -1  
  12.         while l <= r:  
  13.             m = (l+r)//2  
  14.             if target == nums[m]:  
  15.                 return m  
  16.             elif target == nums[r]:  
  17.                 return r  
  18.             elif target == nums[l]:  
  19.                 return l  
  20.             else:  
  21.                 if nums[m] < nums[r]:  
  22.                     if target > nums[m] and target < nums[r]:  
  23.                         l = m+1  
  24.                     else:  
  25.                         r = m-1  
  26.                 else:  
  27.                     if target and target > nums[l]:  
  28.                         r = m-1  
  29.                     else:  
  30.                         l = m+1  
  31.         return -1  

你可能感兴趣的:(leetcode)