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. ...
随机推荐
- Struts2连接Mysql的Crud使用
今天分享的是struts2框架中增删改查的用法: 一:利用Struts2框架 1.1在pom.xml中导入相关依赖 <project xmlns="http://maven.apach ...
- Linux yum安装java环境
1.jdk 1.8 #系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) #安装 ...
- outlook 升级 及邮件同步方式设置
**office(outlook2010 32B)升级到office2016 64B时的操作 1.删除office(excel. word等) 2.选择offcie2016 安装程序安装 (outlo ...
- Thymeleaf 模板使用 Error resolving template "/home", template might not exist or might not be accessible by any of the
和属性文件中thymeleaf模板的配置相关 1.配置信息 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix= ...
- Angular 调试
我们新建一个项目.执行 ng server 会启动一个网站. 1. 执行 where ng .看看ng 是什么. D:\Abp学习\angular\Mytest>where ng C:\User ...
- C++线程同步之原子操作
所谓的原子操作就是指一个线程对于某一个资源做操作的时候能够保证没有其它的线程能够对此资源进行访问. 原子操作仅仅能够解决某一个变量的问题,只能使得一个整型数据做简单算术运算的时候是原子的. 以下案例需 ...
- 获取select标签的自定义属性
$("#ddlUsers").find("option:selected").attr("fullstr"); fullstr就是自定义属性 ...
- Redis二进制安全概念
二进制安全是指,在传输数据时,保证二进制数据的信息安全,也就是不被篡改.破译等,如果被攻击,能够及时检测出来. 二进制安全包含了密码学的一些东西,比如加解密.签名等. 举个例子,你把数据1111000 ...
- Java面试容器,collection,list,set
1.容器指的是可以容纳其他对象的对象. 2.collection/set/list的联系和区别? (1)collection是Java集合顶级接口,存储一组不唯一,无序的对象: (2)list接口 ...
- selenium实现登录百度(自动识别简单验证码)
需要做的工作 0.工程结构 1.代码: ①baidu_login.py import re import os import sys import time import random from se ...