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.

思路:找最小值,然后两边分别二分查找。

最小值找法:1. 只要小于上一个值即可。第一个只要小于最后一个(但是线性方法较慢) 2. 同样利用二分查找。

int binarySearch(int A[], int l, int h, int target) {
while(l <= h) {
int mid = (l+h) >> 1;
if(A[mid] > target) h = mid-1;
else if(A[mid] < target) l = mid+1;
else return mid;
}
return -1;
}
class Solution {
public:
int search(int A[], int n, int target) {
int low = 0, high = n-1, mid = 0;
while(A[low] > A[high]) {
if(low + 1 == high) {
mid = high;
break;
}
mid = (low + high) >> 1;
if(A[mid] < A[high]) high = mid;
else if(A[mid] > A[low]) low = mid;
}
return max(binarySearch(A, 0, mid-1, target), binarySearch(A, mid, n-1, target));
}
};

Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

思路: 同上,除了找最小值时,需要在 A[l] = A[mid] = A[h] 这种情况时,顺序走一遍找中值。

int binarySearch(int A[], int l, int h, int target) {
while(l <= h) {
int mid = (l+h) >> 1;
if(A[mid] < target) l = mid+1;
else if(A[mid] > target) h = mid-1;
else return true;
}
return 0;
}
int getMid2(int A[], int l, int h, int target) {
int id = l;
while(++l < h) {
if(A[l] < A[l-1]) {
id = l;
}
}
return id;
}
int getMid(int A[], int n, int target) {
int l = 0, h = n-1, mid = 0;
while(A[l] >= A[h]) {
if(l + 1 == h) {
mid = h;
break;
}
mid = (l+h) >> 1;
if(A[mid] == A[h] && A[mid] == A[l]) return getMid2(A, l, h, target);
if(A[mid] <= A[h]) h = mid;
else if(A[mid] >= A[l]) l = mid;
}
return mid;
}
class Solution {
public:
bool search(int A[], int n, int target) {
int m = getMid(A, n, target);
return binarySearch(A, 0, m-1, target) || binarySearch(A, m, n-1, target); }
};

49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II的更多相关文章

  1. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  2. eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面

    eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...

  3. Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)

    原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  4. Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)

    原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...

  5. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  6. 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 ...

  7. Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target

    问题:链接 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  8. sorted matrix - search & find-k-th

    sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...

  9. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

随机推荐

  1. python 从文件导入分类

    # -*- coding:utf-8 -*- """ 从文件导入分类 根据行首制表符或空格确定层级关系(4个空格等于一个制表符 同一行制表符和空格不能混用 ) 必须是 u ...

  2. Yii2 主从 数据库

    配置方法 参考资料:http://www.linuxidc.com/Linux/2015-07/120134.htm 读写分离(Read/Write Splitting). 1.原理:让主数据库(ma ...

  3. 再议C++的性能

    最近在公司里的项目做的是性能优化,相关性能调优的经验总结也在前一篇文章里说了.这里再说一说和性能相关的东西.主要针对的是C++类库中常用的一些数据结构,比方说std::string.顺序容器(vect ...

  4. 转:Caused by: java.lang.NoSuchMethodError: org.apache.log4j.Category.log

    Caused by: java.lang.NoSuchMethodError: org.apache.log4j.Category.log出现的异常:java.lang.reflect.Invocat ...

  5. android基础(二)Broadcast Receiver知识

    1.广播的注册 (1)动态广播注册: 优点:可以灵活控制广播的注册与撤销 缺点:必须在程序启动时才能接收广播 IntentFilter :当Intent在组建之间传递时,组件想告诉android系统自 ...

  6. Linq学习总结1--参考Linq技术详解

    2个要点: 1.linq操作的集合必须实现IEnumerable接口,所以在这3.0之前为实现该接口的集合需通过Cast或TypeOf方法转换成可Linq的集合; 2.查询式和Lame那啥表达式都可以 ...

  7. 315Mhz、433Mhz无线遥控信号的解码分析和模拟

    摘要 前段时间学习无线电的同时了解到arduino是作为技能尚未成熟技术宅的我继树莓派又一个不错的选择.于是花了200元购得3块arduino开发板(2*nano&1*uno)和其他传感器等, ...

  8. 三部曲一(数据结构)-1024-Eqs

    解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少.取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解.具体做 ...

  9. Filco minila 的蛋疼。

    3494左shift坏了,期间邮寄厂家维修,就把尘封多年的minila拿出来用着. 最为人诟病的问题:蓝亚适配,与mac跟iphone都能快速的匹配连接上.但是对于我的dell vestro 2012 ...

  10. Android-->Genymotion虚拟机(模拟器)的配置

    --> Genymotion 是一套完整的工具,它提供了Android虚拟环境.它简直就是开发者.测试人员.推销者甚至是游戏玩家的福音. 我只能说非常好用,模拟器中顶级,具体好处可以度娘. -- ...