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的更多相关文章

  1. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

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

  2. [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 ...

  3. 【leetcode】Search in Rotated Sorted Array II

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

  4. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  5. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

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

  7. Search in Rotated Sorted Array II leetcode

    原题链接,点我 该题解题参考博客 和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,出现了比较复杂的case,甚至 ...

  8. [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 ...

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

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

随机推荐

  1. 纯 CSS 绘制图形(心形、六边形等)

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  2. day7

    本节作业: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...

  3. [Sass]混合宏

    [Sass]混合宏-声明混合宏 如果整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的.但当你的样式变得越来越复杂,需要重复使用大段的样式时,使 ...

  4. octave手册

    GNU OCTAVE是一种高级语言,主要用于数值计算.它提供交互式命令行窗口,用于求解线性和非线性问题并计算出数值,并可以进行其它数值实验,还可以用来作为一个批量数据处理语言 运行Ocatve: oc ...

  5. php定界符<<<EOF讲解(转)

    Heredoc技术.可用来输出大段的html和javascript脚本 1.PHP定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西: 2.在PHP定界符中的任何特殊字符都不需要转义:  ...

  6. llinux常用命令

    (1)Ctrl+alt+(1-6)可调用控制台程序 (2)date---可看时间日期 (3)date 月日时分年 ------可修改时间 (4)useradd-------新建用户 (5)passwd ...

  7. [Tool]使用ConfuserEx混淆代码

    为了防止程序发布后被一些"坏人"破解,开发者通常会对自己的代码进行混淆.这篇博客将介绍一款使用很广,并且混淆效果也不错的工具ConfuserEx. 新建一个C# 控制台程序,Hel ...

  8. .NET 缓存模块设计

    上一篇谈了我对缓存的概念,框架上的理解和看法,这篇承接上篇讲讲我自己的缓存模块设计实践. 基本的缓存模块设计 最基础的缓存模块一定有一个统一的CacheHelper,如下: public interf ...

  9. 百度地图API试用--(初次尝试)

    2016-03-17: 百度地图API申请key的步骤相对简单,不做过多阐述. 初次使用百度地图API感觉有点神奇,有些功能加进来以后有点问题,注释掉等有空再解决. 代码如下: <%@ page ...

  10. PHP开发笔记:二维数组根据某一项来进行排序

    比如说我们现在有一个二维数组: $arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => ...