#Suppose a sorted array 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.


class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left=0
right=len(nums)-1
while left <= right:
mid = (left+right)/2
if target == nums[mid]:
return mid
if nums[mid] >= nums[left]:
if target < nums[mid] and target >= nums[left]:
right=mid-1
else:
left=mid+1
elif nums[mid] < nums[right]:
if target > nums[mid] and target <= nums[right]:
left=mid+1
else:
right=mid-1
return -1

leetcode Search in Rotated Sorted Array python的更多相关文章

  1. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  2. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  3. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  4. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. [LeetCode] 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 ...

  6. LeetCode——Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  7. LeetCode: Search in Rotated Sorted Array 解题报告

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

  8. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

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

  9. [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路

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

随机推荐

  1. 源码推荐(7.17):不规则按钮类似于遥控器按钮,一个可以最大程度简化PageView与TabView切换的第三方框架

    不规则按钮,类似于遥控器按钮,可以单独控制按钮的上下左右(作者:masa_chu) 不规则按钮,类似于遥控器按钮,可以单独控制按钮的上下左右 测试环境:Xcode 6.2,iOS 6.0以上 Lazy ...

  2. Objective-C——判断对象等同性

    无论我们使用什么语言,总是会出现需要判断两个对象是否相等的情况,OC当然也不例外.首先看一段代码: NSString *str1 = [[NSString alloc] initWithCString ...

  3. mysql的面试试题

    1, mysql的复制原理以及流程. (1)先问基本原理流程,3个线程以及之间的关联. 答:Mysql复制的三个线程:主库线程,从库I/O线程,从库sql线程: 复制流程:(1)I/O线程向主库发出请 ...

  4. .NET 创建并写CSV文件

    /// <summary> /// 创建并写日志 /// </summary> /// <param name="SuccessA100">&l ...

  5. iOS开发那些事儿(三)JsonKit解析原理

    json_parse_it :开始解析,字符串指针从头到尾循环 jk_parse_next_token:获取下个字符的type和length 大部分分隔符长度都是固定1 jk_parse_string ...

  6. S5700交换机配置端口镜像

    S5700交换机配置端口镜像 <Quidway>system-view    //进入系统视图 Enter system view, return user view with Ctrl+ ...

  7. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  8. [string]字符串中几个比较难的算法和容易搞混的题目

    一.两个难点算法 1.Manacher算法,线性时间求最长回文子串 2.KMP算法,字符串匹配问题,c语言中的strStr 二.几个题目 1.最长回文子串 方法:暴力,动态规划,中心扩展,manach ...

  9. python学习笔记:python对象

    一.python对象 python使用对象模型来存储数据,构造任何类型的值都是一个对象.所有的python对象都拥有三个特性:身份.类型和值. 身份:每个对象都有一个唯一的身份标识自己,对象的身份可以 ...

  10. sql server 2008 索引

    微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonclustered index,也称非聚类索引.非簇集索引) 我们举例来 ...