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. Android SurfaceView

    今天介绍一下SurfaceView的用法,SurfaceView一般与SurfaceHolder结合使用,SurfaceHolder用于向与之关联的SurfaceView上绘图,调用SurfaceVi ...

  2. CSS强制中英文换行与不换行

    1. word-break:break-all; 只对英文起作用,以字母作为换行依据 2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据 3. white-space ...

  3. Python的平凡之路(10)

    异步IO 数据库 队列 缓存 1.Gevent协程 定义:用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下 ...

  4. CBC和CTR解密模式——C++实现

    利用已经封装好的AES加密算法,实现CBC模式加密和CTR模式加密. (1)CBC解密 如图,CBC模式的解密,步骤主要有三个,首先是拿密文段逐一放到AES解密盒子里面得到一个结果temp(事先要把密 ...

  5. 2016 - 1 - 23 xml解析 -- 语法简介

    一: XML的概念 1. 一种可拓展标记语言 2. 与json一样,也是一种常用的数据交互格式 3. 一般也叫XML文档---XML Document 二: XML语法   1.一个完整的XML文档一 ...

  6. RFID Hacking④:使用ProxMark3 破解门禁

    文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 前言 国际黑客大会Defcon传统之一:开锁!因为黑客认为锁也是一种安全挑战.我们在黑客题材电影.电视剧中也常常 ...

  7. SCC(强连通分量)

    1.定义: 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(SC---strongly connected). 有向图中的极大强连通子图,成为强连通分量(SCC---strongly ...

  8. iOS 10 UserNotifications 框架解析

    摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...

  9. JavaScript闭包学习笔记

    此文都是大牛们关于闭包的观点,在此只是总结. 闭包应用的两种情况即可——函数作为返回值,函数作为参数传递. 1 深入理解javascript原型和闭包 判断一个变量是不是对象非常简单.值类型的类型判断 ...

  10. Android FM模块学习之四源码学习(2)

    前几章我们分析了FM模块的几个主要的类文件,今天要分析的是:FMTransceiver.java   // 某些工程中名称为FMRadioService.java public class FmTra ...