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) {
return bs(A,0,n-1,target);
}
int bs(int A[],int left,int right,int target){
if(left<right){
//没有rotated,数组递增
if(A[left]<A[right]){
if(target<A[left]||target>A[right]){
return -1;
}else{
int center=(left+right)/2;
if(A[center]==target)
return center;
else if(A[center]<target){
return bs(A,center+1,right,target);
}else{
return bs(A,left,center,target);
}
}
}else{
int center = (left+right)/2;
if(A[center]==target)
return center;
int index = bs(A,left,center,target);
if(-1==index)
return bs(A,center+1,right,target);
return index;
}
}else if(left==right){
if(A[left]==target)
return left;
return -1;
}
}
};
Search in Rotated Sorted Array的更多相关文章
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 migh ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【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 II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 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 before ...
- Search in Rotated Sorted Array II leetcode
原题链接,点我 该题解题参考博客 和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,出现了比较复杂的case,甚至 ...
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索
11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- Search in Rotated Sorted Array I
Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you befo ...
随机推荐
- Unity3D 响应摇杆
if (Input.GetKeyUp(KeyCode.Joystick1Button0)) { Debug.Log("Joystick1Button0"); } if (Input ...
- nginx配置返回文本或json
有些时候请求某些接口的时候需要返回指定的文本字符串或者json字符串,如果逻辑非常简单或者干脆是固定的字符串,那么可以使用nginx快速实现,这样就不用编写程序响应请求了,可以减少服务器资源占用并且响 ...
- C++内存管理
1. 栈(Stack): 位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效.2. 堆(Heap): 由new申请的内存,由delete负责 ...
- tp5 中 model 的修改器
修改器可以在数据赋值的时候自动进行转换处理 class User extends Model { public function setNameAttr($value){ return strtolo ...
- html5 websocket 实时日志推送
http://blog.csdn.net/neutrojan/article/details/46119645
- Oracle over函数
Oracle over函数 SQL code: sql over的作用及用法RANK ( ) OVER ( [query_partition_clause] order_by_clause )DE ...
- 父元素相对定位后,子元素在ie下被覆盖的问题!
<div id="append_parent" style="position: relative;"> <div id="zoom ...
- .NET 需要处理的高性能WEB架构 - .NET架构
1.如果不想被微软包围(其实微软的一套并不贵,是被谣言传高了),数据层依然可以选择SQL Server数据库和存储过程. 2.缓存不再依赖.net自身提供的缓存机制,迁移到部署在Linux平台上的分布 ...
- Scrapy shell调试网页的信息
通过scrapy shell "http://www.thinkive.cn:10000/zentaopms/www/index.php?m=user&f=login"
- java中被各种XXUtil/XXUtils辅助类恶心到了,推荐这种命名方法
且看一下有多少个StringUtils 列举一下XXUtil/XXUtils恶劣之处 1. 不知道该用XXUtil还是用XXUtils, 或者XXHelper, XXTool 2. 不知道该用a.ja ...