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.

Search in Rotated Sorted Array II,Find Minimum in Rotated Sorted Array,Find Minimum in Rotated Sorted Array II对照看

解法一:顺序查找 just a joke :D

class Solution {
public:
int search(int A[], int n, int target) {
for(int i = ; i < n; i ++)
{
if(A[i] == target)
return i;
}
return -;
}
};

解法二:二分查找

先用二分法找到最大元素,将数组切分成两个有序数组,再进行二分查找

class Solution {
public:
int search(int A[], int n, int target) {
if(n==)
return (target==A[])?:-; //find the maximum first
int low = ;
int high = n-;
while(low < high)
{
int mid = (low+high)/;
if(A[mid] < A[low])
high = mid-;
else if(A[mid] > A[low])
low = mid;
else
{//low+1==high
if(A[high]>A[low])
low = high;
break;
}
}
int ind = low;
//to here, low is the index of maximum
//0~ind, ind+1~n-1 are two sorted arrays
if(target >= A[])
{//first array: 0~ind
low = ;
high = ind;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
else
{//second array: ind+1, n-1
low = ind+;
high = n-;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
}
};

解法三:可处理重复元素的二分查找,即不断去掉low与high元素

class Solution {

public:
int search(int A[], int n, int target) {
int low = ;
int high = n-;
while (low <= high)
{
int mid = (low+high)/;
if(A[mid] == target)
return mid;
if (A[low] < A[mid])
{
if(A[low] <= target && target < A[mid])
//binary search in sorted A[low~mid-1]
high = mid - ;
else
//subproblem from low to high
low = mid + ;
}
else if(A[mid] < A[high])
{
if(A[mid] < target && target <= A[high])
//binary search in sorted A[mid+1~high]
low = mid + ;
else
//subproblem from low to mid-1
high = mid - ;
}
else if(A[low] == A[mid])
low += ; //A[low]==A[mid] is not the target, so remove it
else if(A[mid] == A[high])
high -= ; //A[high]==A[mid] is not the target, so remove it
}
return -;
}
};

解法四:

二分查找,先对mid元素处于前半段还是后半段分情况讨论,再对target元素处于前半段还是后半段分情况讨论。

class Solution {
public:
int search(int A[], int n, int target) {
return search(A, , n-, target);
}
int search(int A[], int left, int right, int target)
{
if(left > right)
return -; if(A[left] < A[right])
// one part, binary search
return binarySearch(A, left, right, target); // else, two part
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[left] > A[mid])
{// mid is in second part
if(target > A[mid])
{// target may be in the first part (case1), or second part after mid(case2)
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
else
{// target is in the second part before mid
return search(A, left, mid-, target);
}
}
else
{// mid is in first part
if(target > A[mid])
{// target is in first part after mid
return search(A, mid+, right, target);
}
else
{// target may be in the first part before mid (case1), or second part
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
}
}
int binarySearch(int A[], int left, int right, int target)
{
while(left <= right)
{
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[mid] > target)
right = mid - ;
else
left = mid + ;
}
return -;
}
};

【LeetCode】33. Search in Rotated Sorted Array (4 solutions)的更多相关文章

  1. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  3. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  4. 【Leetcode】81. Search in Rotated Sorted Array II

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

  5. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  6. 【LeetCode】081. Search in Rotated Sorted Array II

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  7. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  8. 【LeetCode】033. Search in Rotated Sorted Array

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  9. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. CodeM资格赛2

    题目描述 组委会正在为美团点评CodeM大赛的决赛设计新赛制. 比赛有 n 个人参加(其中 n 为2的幂),每个参赛者根据资格赛和预赛.复赛的成绩,会有不同的积分.比赛采取锦标赛赛制,分轮次进行,设某 ...

  2. 220V和380V电器设备电流计算方法

    220V和380V电器设备电流计算方法 1)单相电机电流=功率/(电压*功率因数*效率): 2)三相电机电流=功率/(1.732*电压*功率因数*效率): 3)空载电流为额定电流的30-50%左右: ...

  3. 读CRecordset

    void CDictCol::LoadDictCol(void) { // 加载数据字典信息 CString cstrSql; cstrSql.Format("SELECT dc.TblID ...

  4. .NET:动态代理的 “5 + 1” 模式

    背景 什么叫“动态代理”,代理模式我们都知道,动态代理就是动态生成的代理(采用Emit). 重量级的ORM和IOC产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一 ...

  5. Unity3d 换装 之 模型动画分离

    在手游中换装成了越来越不可缺的一个功能,毫无疑问各式各样的时装为游戏增添了不同的色彩. 对于2D手游,或许是更换对应的序列帧,也或许是如同3D手游一般,更换模型动画. 对于游戏中的人物,一般分为头.上 ...

  6. Mysql 会导致锁表的语法

    最近再找一些Mysql锁表原因,整理出来一部分sql语句会锁表的,方便查阅,整理的不是很全,都是工作中碰到的,会持续更新 笔者能力有限,如果有不正确的,或者不到位的地方,还请大家指出来,方便你我,方便 ...

  7. OpenShift DNS的机制

    为什么不直接用kube-dns? 为什么不直接用kube-dns? 为什么不直接用kube-dns? 感谢各位前辈的专研,在下午有限的时间里把Openshift DNS的机制理了一下.更详细的材料大家 ...

  8. OpenShift 容器日志和应用日志分离问题

    一般来说应用日志和容器日志一样输出到console,这样oc logs的时候就能把所有的获取到,但这种模式下输出的日志比较多,问题定位不方便,更多的时候开发人员只想通过应用日志来查看定位问题就够了,所 ...

  9. nose的setup和teardown

    参考:http://blog.csdn.net/linda1000/article/details/8533349 1.模块的setUp和tearDown def setUp(): print &qu ...

  10. 7.volatile关键字

    volatile:一个线程修改了某一个共享变量的值,其他线程也是否能够立即知道这个修改的 1.主要是让该“变量”在多个线程中可见,在java中每一个线程都有一块自己的工作区,其中就存放着所有线程“共享 ...