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

  • 思路:

  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. [USACO10HOL]赶小猪

    嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...

  2. C/C++语言之由数字26引起的文件的数据保存与读取调试。

    首先在VS2010中遇到的问题是,建立了一个结构体 struct position{ int x; int y; }: 然后用此结构体声明一个数组rout[8]; for(int i=0;i<8 ...

  3. c++问题集合

    1.对于程序未运行和运行后的代码段到底存储什么? 2.编写程序时为什么先申请变量后使用? 3.malloc本质到底分配了什么?谁赋给的? 4.程序在系统上是怎么运行起来的? 5.当我们双击一个程序运行 ...

  4. win10 开启全局代理

    1. 打开设置 2. 点击“网络和Internet” 3.设置手动代理 . 设置完成后就可以愉快的玩耍啦

  5. python 获取远程设备ip地址

    python2.7 #!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - # This program is ...

  6. 银联刷卡POS机冲正

    冲正是为系统认为可能交易失败时采取的补救手法. 即一笔交易在终端已经置为成功标志,但是发送到主机的账务交易包没有得到响应,即终端交易超时,所以不确定该笔交易是否在主机端也成功完成,为了确保用户的利益, ...

  7. tar 命令出现 Cowardly refusing to create an empty archive 问题详解

    错误提示的字面意思是,系统惴惴不安地拒绝执行创建一个空压缩包的任务.检查tar命令的语法!!!参考:https://blog.csdn.net/deniro_li/article/details/54 ...

  8. C++常用字符串函数使用整理

    strlen(字符数组) 功能:求字符串长度. 说明:该函数的实参可以是字符数组名,也可以是字符串. 使用样例: char s1[80] = "China"; cout<&l ...

  9. PyTricks-使用namedtuple以及dataclass的方式定义类

    from collections import namedtuple from dataclasses import dataclass # 以前简单的类可以使用namedtuple实现. Car = ...

  10. MD5介绍

    md5介绍 1. md5简介 md5的全称是md5信息摘要算法(英文:MD5 Message-Digest Algorithm ),一种被广泛使用的密码散列函数,可以产生一个128位(16字节,1字节 ...