【题解】【数组】【查找】【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 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的更多相关文章
- 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 ...
- [Leetcode] Search in Rotated Sorted Array 系列
Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- [Leetcode] search in rotated sorted array ii 搜索旋转有序数组
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 特征值提取之 -- TF-IDF值的简单介绍
首先引用百度百科的话: "TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料 ...
- Group by的使用方法
sql中如果要分组查询,一般都会使用到group by语句,如何熟练使用group by语句呢,我分以下几点进行总结. Group by与聚合函数 Group by与Having 需要注意的地方 Gr ...
- java之代理模式
静态代理: java代理是一种模式--代理模式.采用代理模式,可以在不改变目标类代码的基础上,通过代理对象,来增加额外的功能(比如增加日志检测等)或者只需要目标对象的部分行为. java中,代理分为静 ...
- ROS创建工作空间(三)
查看正在使用的ROS工作空间,使用命令 echo $ROS_PACKAGE_PATH 我新建了两个
- (DFS)zoj1008-Gnome Tetravex
现在zoj暂时关了,实际上是在scuoj上做的. 题目地址 看起来题目比较复杂,实际上主要需要思维的是如何恰当的剪枝及合适的DFS角度. 问题等价于将n*n个可能相同的方块放到一个n*n的表中,使满足 ...
- 凭借K2 SmartObject框架,在SharePoint中集成数据
随着SharePoint 2013的发布,Microsoft已提供Business Connectivity Services(BCS)增强功能以及外部列表功能,确保您可以更简单地在SharePoin ...
- Program E-- CodeForces 18C
Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...
- 深入理解dispatch_queue
Grand Central Dispatch是苹果过去几年创造出来的非常强大的API,在Let's Build系列的最新一期中,我们将探究dispatch_queue基础功能的重新实现.该主题是Rob ...
- 推荐一款免安装的在线Visio流程工具ProcessOn
昨天收到一人的邮件,说某个软件叫ProcessOn是web版的visio,出于对技术知识的渴望以及自己的好奇所以对ProcessOn进行了一番体验.结果有点被这个软件给吸引上了,无论是在用户体验上,还 ...
- wince6.0 开机启动定制的程序
1.prject.bib MediaApp.exe $(_FLATRELEASEDIR)\MediaApp.exe NK H MediaApp.lnk $(_FLATRELEASEDIR)\Media ...