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. ...
随机推荐
- Linux虚拟机常用命令
参考原文链接:(https://blog.csdn.net/fanyun_01/article/details/51083585) 一.Linux虚拟机常用命令 # virsh list //查看已打 ...
- 『Python基础』第1节 Windows环境下安装Python3.x
一. Python安装 1. 下载安装包 https://www.python.org/downloads/release/python-374/ # 3.7安装包 # 如需安装python2.7版本 ...
- const函数返回自身的引用也是常量引用
const函数返回自身的引用也是const 解决:根据对象是否为consr重载
- “Using 声明”在 C# 7.3 中不可用。请使用 8.0 或更高的语言版本。
Core3.0升级至3.1时候报错:“Using 声明”在 C# 7.3 中不可用.请使用 8.0 或更高的语言版本. 参照微软文档:https://docs.microsoft.com/zh-cn/ ...
- Redis的bind的误区(转)
原文1:https://blog.csdn.net/cw_hello1/article/details/83444013 原文2:https://www.cnblogs.com/suiyueqiann ...
- jacascript Date 学习
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! Date dateObject.getDate(); 返回一个月中的某一天(1-31) dateObje ...
- 15-MySQL DBA笔记-运维管理
第15章 运维管理 随着各种技术的快速发展,现今的DBA可以比以前的DBA维护多得多的数据库实例.DBA已经越来越像一个资源的管理者,而不是简单的操作步骤执行人.本章将为读者介绍规模化运维之道.首先, ...
- jvm的内存区域介绍
什么是jvm? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...
- win10 amd显卡开机黑屏很久
转载自:https://jingyan.baidu.com/article/3c48dd34844e0ce10ae35865.html 升级win10后,使用a卡的小伙伴应该会大为恼火,开机竟然需要黑 ...
- Visual Studio 开发大量 JavaScript 代码项目程序崩溃的解决方案
最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...