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

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

1) 这个题目思路就是先画图, 分区间讨论,注意是左移还是右移,最好画图判断下。

2)可以利用[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search 的思路,通过lg n 找到转折点,然后分别在两段里面找。

T: O(lgn)

Code

1)

class Solution:
def search(self, nums, target):
if not nums: return -1
S, E, l, r = nums[0], nums[-1], 0, len(nums) - 1
while l + 1 < r:
mid = l + (r - l)//2
if target == nums[mid]: return mid
if nums[mid] >= S:
if S <= target <= nums[mid]:
r = mid
else:
l = mid
else:
if nums[mid] <= target <= E:
l = mid
else:
r = mid
if nums[l] == target: return l
if nums[r] == target: return r
return -1

[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search的更多相关文章

  1. [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search

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

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

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

  3. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  4. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  5. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  6. 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree

    Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...

  7. Convert Sorted List to Binary Search Tree&&Convert Sorted Array to Binary Search Tree——暴力解法

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  8. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  9. [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

随机推荐

  1. lua中table的遍历,以及删除

    Lua 内table遍历 在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: 1. ipairs for index, value in ipair ...

  2. SQL Server 2016新特性:数据库级别配置

    新的  ALTER DATABASE SCOPED CONFIGURATION (Transact-SQL) 用来配置数据库级别配置. 这个语句可以配置每个数据库的配置: 清理过程cache 设置MA ...

  3. Asp.Net WebApi上传图片

    webapi using System; using System.Collections; using System.Collections.Generic; using System.Diagno ...

  4. Git命令速查表

  5. Java中Access restriction:····的解决方法

    http://blog.csdn.net/bit2012_2015/article/details/22798779 ————————————————————————————————————————— ...

  6. R par yaxp xaxp 显示x轴和y轴的刻度线

    R语言会自动根据数据的范围,在X轴和Y轴上标记合适的刻度 > options(scipen = ) > plot(sample(:, )) 生成的图片如下 通过par("yaxp ...

  7. C# 3个延时函数

    ) { int time = Environment.TickCount; while (true) { if (Environment.TickCount - time >= DelayTim ...

  8. Java锁机制(一)synchronized

    进行多线程编程的时候,需要考虑的是线程间的同步问题.对于共享的资源,需要进行互斥的访问.在Java中可以使用一些手段来达到线程同步的目的: 1. synchronized 2. ThreadLocal ...

  9. 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息

    sql脚本: SELECT TableName then d.name else '' end,---表名 TableShowsThat then isnull(f.value,'') else '' ...

  10. MYSQL 内存模型