49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
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的更多相关文章
- Why is processing a sorted array faster than an unsorted array?
这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...
- eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面
eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...
- Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)
原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
- Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)
原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
- 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 ...
- 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 ...
- sorted matrix - search & find-k-th
sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...
- 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 ...
随机推荐
- Servlet里面url-pattern的通配符*的使用规则
简单来说: 以”/’开头和以”/*”结尾的是用来做路径映射的. 以前缀”*.”开头的是用来做扩展映射的. “/” 是用来定义default servlet映射的. 剩下的都是用来定义详细映射的.比如: ...
- PowerShell脚本:随机密码生成器
脚本名称:s随机密码生成器_v2.63.ps1脚本作用:产生随机密码.每密码字符个数,密码数量,存盘位置等可以自定义.脚本用法:脚本采用了硬编码,所以你需要打开脚本,修改如下变量:$生成密码总个数 = ...
- iOS 图片填充 UIImageView (contentMode)
掐指算下来做iOS开发也是有两年多的时间了,然后今天一个超级常用的控件让我颜面大跌,于是我准备把自己的丢人行径公之于众.如果您看到我这篇文章时和我一样,也是刚刚知道这项功能,那么您就当收获了一个... ...
- vim常用命令总结 (转)
vim 选择文本,删除,复制,粘贴 文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v 从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V 从光标 ...
- sqoop的增量导入(increment import)
1.import增量导入的官方说明
- UVA 10407 差分思想的运用
就是每两项相减,肯定能被模数整除. #include <iostream> #include <cstring> #include <cstdio> #includ ...
- Windows Squid 安装配置
squid 可以做反向代理将系统中相对静态的页面进行缓存和负责均衡,提高网站访问速度,增强网站可用性.安全性.用户访问Squid 反向代理服务器的 IP 地址,这样客户端的 URL 请求将被发送到反向 ...
- JM8.6学习
1. vs2010 设置参数 编译运行JM8.6 (参考http://bbs.chinavideo.org/forum.php?mod=viewthread&tid=15695&hig ...
- 一种工业级系统交互建模工具的应用--EventStudio System Designer
一种工业级系统交互建模工具的应用 [摘要] 本文以探索如何维护大规模复杂系统交互设计模型为目的,以EventHelix公司的商业付费软件EventStudio System Designer为建模工具 ...
- Python::OS 模块 -- 文件和目录操作
os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...