• 题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值;

  • 思路:

  1. 第一遍二分遍历,找到数组中最小值的索引;
  2. 第二遍分别对最小值左右两边的数组进行二分查找;
class Solution(object):

    def find_min(self, nums):
if not nums:
return -1
left, right = 0, len(nums) - 1
while nums[left] > nums[right]:
if right - left == 1:
return right
mid = (left + right) / 2
if nums[left] <= nums[mid]:
left = mid
if nums[right] >= nums[mid]:
right = mid
return 0 def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
min_index = self.find_min(nums)
if nums[min_index] == target:
return min_index
elif nums[min_index] > target:
return -1
else:
left = self.search_t(nums, 0, min_index, target)
if left >= 0:
return left
right = self.search_t(nums, min_index, len(nums)-1, target)
if right >= 0:
return right
return -1 def search_t(self, nums, left, right, target):
while left <= right:
mid = (left + right) / 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

Python 解LeetCode:33. Search in Rotated Sorted Array的更多相关文章

  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. NOIP考前总结

    最近出的锅比较多啊,我来总结一下吧 $1.$小心文件名/文件输入输出!别打错了!结束前十分钟一定要检查! $2.$开数组前要算好内存,不要开一个$1e8$或$1e4*1e4$这样的大数组,直接GG $ ...

  2. E.Substring Reverse Gym - 101755E

    Substring Reverse Problem Two strings s and t of the same length are given. Determine whether it is ...

  3. linux中如何修改最大文件句柄数

    1.使用ulimit -a可以查看,其中的open files后面的数就是最大文件句柄数 2.临时方法:使用ulimit -n size修改最大文件句柄数(这种方法只针对当前进程有效) 3.永久方法: ...

  4. easyui复选框实现单选框

    $(':checkbox[name=primary_key_flag]').each(function(){ $(this).click(function(){ if(this.checked){ $ ...

  5. IntelliJ跳转到抽象方法的实现

    ctrl + b (等价于ctrl + 鼠标点击方法名)会调到这个类型的抽象方法中: 如果想要跳转到这个方法的具体实现可以使用 ctrl + alt + 鼠标点击方法名. IntelliJ快速查找一个 ...

  6. 【Phoenix】1、搭建 Phoenix 环境

    Ps: 需要注意的是,我学习的时候,Elixir 是 1.8.1的版本,而 Phoenix 是 1.4.1的版本,对于其他版本,不一定正确. 1.安装 Phoenix 之前,先安装 Elixir. 2 ...

  7. [转][C#]基础连接已经关闭 未能为 SSL/TLS 安全通道建立信任关系

    来自:https://www.cnblogs.com/waw/p/8286608.html 代码部分: static TestApplication(){ ServicePointManager.Se ...

  8. ajax传参数json对象到后台获取

    类型1 var version = $("#version").val(); var ids[i] = ("127.0.0.1","192.168.1 ...

  9. kotlin函数的参数和返回值

    fun main(arg: Array<String>) { val asList = asList(, , , , , ) println(asList) } fun <T> ...

  10. 图解 HTTP 笔记(三)—— HTTP 报文内的 HTTP 信息

    本章主要讲解请求和响应是如何运作的 一.HTTP 报文 用于 HTTP 协议交互的信息被称为 HTTP 报文,客户端的 HTTP 报文叫做请求报文,服务器端的叫做响应报文. HTTP 报文大致可分为报 ...