二分查找算法以及旋转之后的数组二分查找算法:

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'author' class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
return self.sorted_binary_search(nums, 0, len(nums) - 1, target) #二分查找算法
def binary_search(self, nums, start, end, targrt):
if start > end:
return -1
mid = start + (end - start)//2
if nums[mid] > targrt:
return self.binary_search(nums, start, mid - 1, targrt)
elif nums[mid] < targrt:
return self.binary_search(nums, mid + 1, end, targrt)
else:
return mid #旋转数组的折半查找算法
'''
要解决这道题,需要明确rotated sorted array的特性,
那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。
接下来就只需要按照这个特性继续写下去就好。 如果target比A[mid]值要小
如果A[mid]右边有序(A[mid]<A[high])
那么target肯定不在右边(target比右边的都得小),在左边找
如果A[mid]左边有序
那么比较target和A[low],如果target比A[low]还要小,
证明target不在这一区,去右边找;反之,左边找。 如果target比A[mid]值要大
如果A[mid]左边有序(A[mid]>A[low])
那么target肯定不在左边(target比左边的都得大),在右边找
如果A[mid]右边有序
那么比较target和A[high],如果target比A[high]还要大,
证明target不在这一区,去左边找;反之,右边找。
'''
def sorted_binary_search(self, nums, start, end, targrt):
if start > end:
return -1
mid = start + (end - start)//2
if targrt == nums[mid]:
return mid
if nums[start] <= nums[mid]:
#如果左侧有序
if nums[start] <= targrt and targrt <= nums[mid]:
#如果target在左侧中
return self.sorted_binary_search(nums, start, mid - 1, targrt)
else:
return self.sorted_binary_search(nums, mid + 1, end, targrt)
else:
#如果右侧有序
if nums[mid] <= targrt and targrt <= nums[end]:
#如果target在右侧中
return self.sorted_binary_search(nums, mid + 1, end, targrt)
else:
return self.sorted_binary_search(nums, start, mid - 1, targrt) if __name__ == '__main__':
s = Solution()
print(s.binary_search([1,2,3,4,5,6,7,8,9], 0, 8, 9))

代码中注释参考了:http://www.cnblogs.com/springfor/p/3858140.html , 该博客中的算法描述比较易懂

Leetcode-33-Search in Rotated Sorted Array (Hard)的更多相关文章

  1. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  3. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

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

  4. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

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

  5. leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  6. LeetCode 33.Search in Rotated Sorted Array(M)

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

  7. leetcode 33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. Java [leetcode 33]Search in Rotated Sorted Array

    题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  9. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

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

  10. LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)

    题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description   Problem :当前的数组 ...

随机推荐

  1. sqlclr创建表值函数案例

    ----------------------------------------------:定义表值类型:(1.一定返回的是IEnumerable2.一定带参数3.一定有FillRowMethodN ...

  2. 代码阅读软件kscope源码安装指导

    安装 kscope-1.6.2 1. ./configure --without-arts --prefix=/soft/kscope-1.6.2  (I customize the installi ...

  3. Lex Yacc手册

    Python Lex Yacc手册 本文是PLY (Python Lex-Yacc)的中文翻译版.转载请注明出处.这里有更好的阅读体验. 如果你从事编译器或解析器的开发工作,你可能对lex和yacc不 ...

  4. iOS基础 - 多媒体

    一.播放视频 iOS提供了叫做MPMoviePlayerController.MPMoviePlayerViewController的两个类,可以用来轻松播放视频 YouTobe就是用MPMovieP ...

  5. c# in deep 之委托

    通俗来讲,委托就是吩咐别人去做某件事,但不知道他具体会怎么做.使用委托必须注意的一个问题是内存泄露问题:假如委托实例本身不能被回收,委托实例会阻止他的目标被作为垃圾回收.尤其是假如某“短命”的对象调用 ...

  6. 最短路模板[spfa][dijkstra+堆优化][floyd]

    借bzoj1624练了一下模板(虽然正解只是floyd) spfa: #include <cstdio> #include <cstring> #include <alg ...

  7. golang 之 bson 与 struct 转换

    bson的介绍不说了golang下的解析包找到2个 一个是mongo的http://labix.org/gobson,另外一个比较小众https://github.com/sbunce/bson这里用 ...

  8. office文件的预览

    使用FlexPaper实现office文件的预览(C#版) 需求很简单,用户上传office文件(word.excel.ppt)后,可以预览上传的这些文件.搜索的相关的资料后.整理如下: Step1. ...

  9. IOS学习之路十七(通过delegate进行页面传值)

    加入有A B两个页面,要实现从A跳到B的时候把值传过去,现在用delegate协议来实现 在A中定义一个协议,定义一个实现该协议的属性变量 在B中定义一个值(要获得的值类型)和set方法. 要传值B ...

  10. SSH整合之_架构的历史序列图

    只用jsp最原始的架构 jsp+DB的2层架构 jsp+DB+_Entity的2层架构 jsp+DB+_Entity3_+Service的三层架构 jsp+DB+_Entity3_+Service_H ...