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. matlab report generator

    Programmatic report creation Create report content Mlreportgen.dom.Document.append Mlreportgen.dom.P ...

  2. linux copy

    cp -ri 131115-6/* /web/www/attachment/  把13这个文件夹下面所有的文件和文件夹复制到 /web/www/attachment cp -a  a  /web/ww ...

  3. 为什么要使用 F#?

      对于小部分 .NET 程序员来说,学习一门 .NET Framework 函数化语言无疑将使自己在编写功能强大软件方面前进一大步.而对其他程序员来说,学习 F# 的理由就因人而异了.F# 能为开发 ...

  4. centos ssh配置使用

    配置 数据阶梯 CentOS SSH配置 默认CentOS已经安装了OpenSSH,即使你是最小化安装也是如此.所以这里就不介绍OpenSSH的安装了. SSH配置: 1.修改vi /etc/ssh/ ...

  5. java 面向对象编程--第17章 I/O系统

    1.I/O操作指的是输入和输出流的操作.相对内存而言,当我们从数据源中将数据读取到内存中,就是输入流,也叫读取流.当我们将内存中处理好的数据写入数据源,就是输出流,也叫写入流. 2.流按照内容分类:字 ...

  6. 使用AlarmManager定时更换壁纸----之一

    import android.os.Bundle;import android.app.Activity;import android.app.AlarmManager;import android. ...

  7. 使用Vibrator控制手机振动

    import android.os.Bundle;import android.os.Vibrator;import android.app.Activity;import android.app.S ...

  8. java正则表达式之java小爬虫

    这个java小爬虫, 功能很简单,只有一个,抓取网上的邮箱.用到了javaI/O,正则表达式. public static void main(String[] args) throws IOExce ...

  9. 优化MYSQL数据库的方法

    1.选取最适用的字段属性,尽可能减少定义字段长度,尽量把字段设置NOT NULL,例如'省份,性别',最好设置为ENUM2.使用连接(JOIN)来代替子查询: a.删除没有任何订单客户:DELETE ...

  10. JavaScript 字符串和日期内容整理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...