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. JDE隐藏Constant等(Hide Object)

    Grid中隐藏列,列值可以使用属性配置,但是列表头Constant需要用函数修改,如下所示:

  2. EF 7 Code First

    加载方式三种 1. Eager Loading 2. Lazy Loading 3.Explicit Loading 使用EF在与关系型数据库的交互中不可避免地需要加载数据,如何加载数据变得至关重要. ...

  3. RAID在数据库存储上的应用-转

    随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...

  4. 解决apache AH01630: client denied by server configuration错误

    昨天给公司配置了apache-2.4.9的版本,今天他们要求把虚拟主机配置起好放网站程序,在修改apache-2.4.9的配置文件中,我发现了2.4.x跟以前的2.2.x里面的很多配置都不一样了,比如 ...

  5. HookIAT的启动程序

    // 启动程序.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...

  6. JS桌面应用

    一.图片预加载 var oImg = new Image(); oImg.onload=function(){ //alert('success'); } oImg.onerror=function( ...

  7. WDCP安装常用组件(memcache、mysqli、PDO_MYSQL、mysql innodb、libmcrypt、php zip)的方法

    为有更好的性能,也为更简洁的系统,一些不是常用或不是基本的功能,都将做为可选的安装组件需要用到的就安装 注意:如果安装时遇到 ./configure: Permission denied提示,很有可能 ...

  8. ioinc

    ioinc setup sassnpm installionic serve cordova plugin add cordova-plugin-crosswalk-webview 十.开发流程 1. ...

  9. LICEcap

    LICEcap是一款简洁易用的动画屏幕录制软件,它可将屏幕录像的内容直接保存为高质量(每帧颜色数量可超过256)GIF动态图片格式.并且支持特别标记鼠标操作动态效果.

  10. C++-static的用法

    static成员变量 为什么要有静态成员变量:1)不进入全局名字空间.2)实现信息隐藏 要点: 除了整型的const 静态成员变量可以在类体内初始化,其它值都需要在类体外的实现文件定义 static函 ...