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. Windows Server 2012 R2桌面化详细设置图解

    一.任务栏左下角启动服务器管理器,然后进行设置. 1.登录不显示服务器管理器 2.本地服务器,看到右边的IE增强的安全配置,如图所示,关闭两项内容.这样就关闭了IE增强安全提示框. 3.“工具”菜单, ...

  2. 《Neural Network and Deep Learning》_chapter4

    <Neural Network and Deep Learning>_chapter4: A visual proof that neural nets can compute any f ...

  3. 伪元素::after和::before

    ::after是一个CSS伪元素,使用::after,你可以从CSS里往页面上新增内容(不再要在HTML里有相应的东西).虽然最终生成的东西并不是真正的DOM里的内容,但这些内容能像普通内容一样显示, ...

  4. js 函数

    函数:封装了某一块功能 四要素: 1.返回类型 2.函数名 3.参数列表4.函数体强类型语言 返回类型 函数名 首字母大写 参数列表string(字符串) Show (int a){ 函数体 }弱类型 ...

  5. 113使用FOR做九九税法表-不知道为什么自己的编译器实现不了制表符

    package com.chongrui.test;/* *使用FOR做九九税法表 *  *  * */public class test {    public static void main(S ...

  6. GRUB、MBR名词解释

    GRUB:是一个来自GUN项目的多操作系统启动程序,是多启动规范的实现,他允许用户在计算机内同时拥有多个操作系统,并在计算机启动时选择希望的操作系统.GRUB可用于选择系统分区上的不同内核,也可用于向 ...

  7. leetcode 137[转]

    没思路.网上找到的. 1. 将每一个int看成32位数,统计每一位出现的次数对3取余,所以需要开辟一个32大小的数组来统计每一位出现的次数 2. 对第一种思路进行简化,模拟3进制: three two ...

  8. OSError: libcudart.so.7.5: cannot open shared object file: No such file or directory

    在ubuntu14.04-64-bit,安装完cuda ,cudnn.opencv后, 配置完MXNet,运行demo 时出现错误,库路径环境变量问题,解决方法: sudo ldconfig /usr ...

  9. Ubuntu菜鸟入门(五)—— 一些编程相关工具

    一.sublime text3 sudo add-apt-repository ppa:webupd8team/sublime-text- sudo apt-get update sudo apt-g ...

  10. Android 谈谈封装那些事 --BaseActivity 和 BaseFragment(二)

      1.前言 昨天谈了BaseActivity的封装,Android谈谈封装那些事--BaseActivity和BaseFragment(一)有很多小伙伴提了很多建议,比如: 通用标题栏可以自定义Vi ...