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. 配置Windows 2008 R2 防火墙允许远程访问SQL Server 2008 R2

    1.先修改 sql server 2008R2的端口号吧,1433经常成为别人入侵的端口,在sql server 配置管理器 -->sql server 网络配置-->MSSQLSERVE ...

  2. ListFiles():返回Files类型数组,可以用getName()来访问到文件名。

    List():显示文件的名(相对路径) ListFiles():返回Files类型数组,可以用getName()来访问到文件名. 使用isDirectory()和isFile()来判断究竟是文件还是目 ...

  3. Cesium高度解析

    var viewer = new Cesium.Viewer('cesiumContainer', { shadows : true }); //为true时,球体会有高程遮挡效果(在没有地形时候也会 ...

  4. sparksql udf自定义函数中参数过多问题的解决

    在进行spark sql数据库操作中,常常需要一些spark系统本身不支持的函数,如获取某一列值中的字符串. 如要获取 “aaaakkkkk”中的第4-第8个字符. 针对这种需求,只有设置UDF来实现 ...

  5. 关于linux - Centos 7 系统下使用PXE网络的方式(pxe+dhcpd+tftp+httpd)安装操作系统

    PXE(Pre-boot Execution Environment)是由Intel设计的协议,它可以使计算机通过网络而不是从本地硬盘.光驱等设备启动. 现代的网卡,一般都内嵌支持PXE的ROM芯片. ...

  6. Nest.js WebSocket

    Docs: https://docs.nestjs.com/websockets/gateways λ yarn add @nestjs/websockets λ nest g ga events e ...

  7. linux的基本操作(NFS服务配置)

    服务配置 [什么是NFS] NFS会经常用到,用于在网络上共享存储.这样讲,你对NFS可能不太了解,笔者不妨举一个例子来说明一下NFS是用来做什么的.假如有三台机器A.B.C,它们需要访问同一个目录, ...

  8. JS 进阶知识点及常考面试题

    将会学习到一些原理相关的知识,不会解释涉及到的知识点的作用及用法,如果大家对于这些内容还不怎么熟悉,推荐先去学习相关的知识点内容再来学习原理知识. 手写 call.apply 及 bind 函数 涉及 ...

  9. ArcGIS AddIN开发之 设置当前工具为Edit Tool

    在GIS数据处理中,经常需要选择要素,再进行操作.所以,为了处理的方便,可以将当前工具处理结束后,将当前工具设置为Edit Tool,以方便下一次的选择处理. 相关资料: 1.ArcMap Name ...

  10. 怎么用js实现jq的removeClass方法

    1.addClass:为指定的dom元素添加样式. 2.removeClass:删除指定dom元素的样式. 3.toggleClass:如果存在(不存在),就删除(添加)一个样式. 4.hasClas ...