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.

思路:

Point 1

这道题很常见,有三个点需要考虑

1 handle edge case. Thinking about only 2 elements in array.

2 The solution will degrade into O(n) only when there is duplicate elements in array 
Searching an Element in a Rotated Sorted Array

For example, for input “1 2 1 1 1 1″, the binary search method below would not work, as there is no way to know if an element exists in the array without going through each element one by one.

3 Merge the 2 steps: find the rotation pivot O( log N) + binary searchO( log N)

举个栗子看看

Look at the middle element (7). Compare it with the left most (4) and right most element (2). The left most element (4) is less than (7). This gives us valuable information — All elements in the bottom half must be in strictly increasing order. Therefore, if the key we are looking for is between 4 and 7, we eliminate the upper half; if not, we eliminate the bottom half.
When left index is greater than right index, we have to stop searching as the key we are finding is not in the array.

 

Point 2

如果需要进行多次查找,完全可以记下来pivot的地址,那么以后就都可以O(lgn)啦

Point 3

翻了翻题解,发现挺有意思的是brute force在Leetcode OJ上也能Accept,这就要从cache hit rate讲起了:

It is difficult to differentiate between O(n) and O(log n) algorithm in general, as @loick already answered nicely here.

Since the O(n) algorithm traverses the array in sequence, it is extremely fast as the cache hit rate is going to be high.

On the other hand, the O(log n) binary search algorithm has more unpredictable array index access, which means it will result in more cache misses.

Unless n is extremely large (up to billions, which is unpractical in this case), there could be a chance that the Brute Force O(n) algorithm is actually faster.

【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array的更多相关文章

  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 系列

    Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...

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

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

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

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

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

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

  6. [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 ...

  7. [Leetcode] search in rotated sorted array ii 搜索旋转有序数组

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

  8. LeetCode——Search in Rotated Sorted Array II

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

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

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

  10. LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索

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

随机推荐

  1. C#识别验证码技术-Tesseract

    相信大家在开发一些程序会有识别图片上文字(即所谓的OCR)的需求,比如识别车牌.识别图片格式的商品价格.识别图片格式的邮箱地址等等,当然需求最多的还是识别验证码.如果要完成这些OCR的工作,需要你掌握 ...

  2. VBA读取固定文件夹中txt内容

    Sub OneTxt() '打开一个txt文件 Dim Filename As Variant, extLine&, mArr() As String Dim i%, j%, txtpath ...

  3. YouTube技术架构

    谈不上翻译,就是摘录 1 billion video views per day 1.Apache 2.Python 3.Linux (SuSe) 4.MySQL 5.psyco, a dynamic ...

  4. Resume Hook SSDT

    在HookSSDT中  通过在第4部通过索引将NtOpenProcess 换成 Base[索引] = FakeNtOpenProcess; so 在阻止时应该在ntoskrnl.exe 找到真正的Op ...

  5. 如何理解java中的变量和常量

    int a =10;这是一个变量,在后面的代码中你可以去更改a的值但如果你在声明a的时候加上了final,那么a就成了常量,后面的代码是不允许对a做修改的.还有一点你要注意,被final修饰的常量必须 ...

  6. libpcap文件格式分析

    第一部分:PCAP包文件格式 一 基本格式: 文件头 数据包头数据报数据包头数据报...... 二.文件头: 文件头结构体  sturct pcap_file_header  {       DWOR ...

  7. 一模 (3) day1

    第一题: 题目大意:给出m个小于n的数,求出出现次数大于m div 2 的数. 1<=n<=2^31   1<=m<=10000 解题过程: 1.看到m的数据范围比较小,直接  ...

  8. uboot启动 及命令分析(3)

    u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char     *name;   /* ...

  9. move

    <span id="span{{$index}}" ng-click="goTab({{$index}})" ng-class="{tabFon ...

  10. Javascript ----字符串(String)中的方法

    涉及字符串时,常用到的几个方法... --------------------------------------------------------------------------------- ...