leetcode解题报告(3):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.
分析
这道题目不难,主要是性能问题。常规解法如下:
class Solution{
public:
int search(int A[],int n,int target){
for(int i = 0; i != n; ++i){
if(A[i] == target) //if found
return i; //return its index
}
return -1; //otherwise return -1
}
}
该算法时间复杂度为O(n),空间复杂度为O(1)。
事实上用二分查找算法会获得更好的性能。
由于本题的有序数组是经过“旋转”的,因此只是部分有序。
以题目所给数组为例,[4 5 6 7 0 1 2]在经过旋转后,整个数组变得无序,但是0两侧的部分数组是有序的。根据二分算法的思想,进行查找的步骤如下:
若A[first] < A[mid],则说明[first,mid)这个区间是有序的,再在这个区间内进行二分查找:
若A[first] <= target 并且target < A[mid] ,则target必在此有序区间中,修改last的值为mid;
否则,说明target在另一半区间(可能是无序的),修改first的值为mid+1;
若A[first] >= A[mid],则说明该区间无序(那么另一区间是有序的),同样在这个区间进行二分查找:
若A[mid] < target 并且 target <= A[last - 1],则说明target在另一半有序区间中,修改first的值为mid+1(注意这一步,比较的是另一半有序区间);
否则,说明target在本区间(是无序的),修改last的值为mid;
重复以上过程,直到找到target或遍历结束(first == last);若遍历结束仍未找到,返回-1,否则,返回mid。
该算法的重点在于,必须要对两个区间分别进行查找,并且只在有序区间对target进行二分查找(旋转过后必然会有一区间有序,一区间无序,而不可能同时无序),target要么在有序的一部分,要么在无序的一部分,要么不存在。且若能找到target,必然会是mid的下标
class Solution{
public:
int search(int A[],int n,int target){
int search(int A[],int n,int target){
int first = 0,last = n;
int mid = (first + last) / 2;
while(first != last){
if(A[mid] == target) //if found,the target value's index must be mid
return mid;
if(A[first] < A[mid]){ //if ordered
if(A[first] <= target && target < A[mid]) //if target is in [first,mid)
last = mid;
else //if not
first = mid + 1;
}else{ //if unordered
if(A[mid] <= target && target < A[last - 1]) //if in [mid,last-1)
first = mid + 1;
else //otherwise
last = mid;
}
}
return -1; //if not found,return -1
}
}
}
该算法时间复杂度为
O(log_2n)
空间复杂度为O(1)。
(题目没有说明该数组是递增有序还是递减有序,真奇怪。这里默认递增。)
leetcode解题报告(3):Search in Rotated Sorted Array的更多相关文章
- leetcode第32题--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
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- LeetCode(33)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 m ...
- leetcode个人题解——#33 Search in Rotated Sorted Array
思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...
- 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 & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
随机推荐
- Idea中一个服务按多个端口同时启动
1.勾选并行启动 2.-Dserver.port=9018
- JS 04 Date_Math_String_Object
Date <script> //1.Date对象 var d1 = new Date(); //Thu May 02 2019 14:27:19 GMT+0800 (中国标准时间) con ...
- Crossword Expert CodeForces - 1194F (期望)
大意: $n$个题, 按照第$i$题随机$t_i$或$t_i+1$秒钟完成, 最多做$T$秒, 求做题数期望. 期望转为做题数$\ge x$的方案数之和最后再除以总方案数 这是因为$\sum\limi ...
- BZOJ4566 HAOI2016找相同字符(后缀自动机)
对第一个串建SAM,第二个串在上面跑,记录当前前缀匹配的最长后缀长度l,每次考虑当前前缀的贡献,对于当前所在节点显然是|right|*(l-len[fa]),而对于其parent树上所有祖先的贡献显然 ...
- WPF 自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分)
#region - 用于绑定ViewModel部分 - public ICommand Command { get { return (ICommand)GetValue(CommandPropert ...
- 验证 vector = 是深拷贝还是浅拷贝
#include <vector> using namespace std; int main() { int w=1920; int h = 1080; vector<int> ...
- 关于element ui滚动条使用
element ui 自带的滚动条使用 在容器的直接外层添加 (需要展现滚动条的那一级) <el-scrollbar style="height:100%"></ ...
- vue-filters(过滤器)
局部过滤器: <html> <head> <title>vue</title> <meta charset="utf-8"&g ...
- 关于jQuery的源码学习
注:该思维学习自另一个博客:https://blog.csdn.net/software0017/article/details/80317348 以下为我自己总结的jQuery结构:
- 《浏览器工作原理与实践》<11>this:从JavaScript执行上下文的视角讲清楚this
在上篇文章中,我们讲了词法作用域.作用域链以及闭包,接下来我们分析一下这段代码: var bar = { myName:"time.geekbang.com", printName ...