题目描述

给出一个转动过的有序数组,你事先不知道该数组转动了多少
(例如,0 1 2 4 5 6 7可能变为4 5 6 7 0 1 2).
在数组中搜索给出的目标值,如果能在数组中找到,返回它的索引,否则返回-1。
假设数组中不存在重复项。

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 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

输入

复制

[1],0

输出

复制

-1


class Solution {
public:
    int search(int A[], int n, int target) {
        if(A == nullptr||n==0)
            return -1;
        int left = 0;
        int right = n-1;
        while(left<=right){
            int mid = left + (right - left)/2;
            if(A[mid] == target)
                return mid;
            if(A[left] <= A[mid]){
                if(A[left]<=target&&A[mid]>target)
                    right = mid-1;
                else
                    left = mid+1;
            }
            else{
                if(A[mid]<target&&A[right]>=target)
                    left = mid+1;
                else
                    right = mid-1;
            }
        }
        return -1;
    }
};

最新文章

  1. Redis集群搭建1
  2. 【原】Python 用例:二进制写入和读取文件内容
  3. JVM之类加载器中篇
  4. Json.net 忽略实体某些属性的序列化
  5. tomcat文件服务配置
  6. 调试CRM JS开发
  7. HTML5 game engines
  8. CentOS 6.4 中安装部署 Nutch 1.7
  9. Samza在YARN上的启动过程 =》 之二 submitApplication
  10. Memcached源码分析——hash
  11. day04
  12. Flex中如何通过设置GridLines对象的horizontalAlternateFill样式交错显示LineSeries图表背景颜色的例子
  13. PHP初入,基础知识点整理(样式表&amp;选择器的使用整理)
  14. FaaS技术框架
  15. openlayers3 基础(常见方法,类及实现)
  16. cnpm install -g live-server 安装服务
  17. Leetcode:204
  18. npm 全局执行 update 、 outdated 出现 npm-debug.log 404 错误的问题
  19. UITableViewCell 获取当前位置
  20. Liunx软Raid实现

热门文章

  1. 【题解】[CQOI]动态逆序对
  2. 手写一个类SpringBoot的HTTP框架:几十行代码基于Netty搭建一个 HTTP Server
  3. fastjson配置序列化过滤转换
  4. 【linux】基础命令一
  5. ztree通过ajax加载json数据中文乱码的解决方法:springmvc配置
  6. python与嵌入式的火花
  7. .net 手动建DataTable 获取DataTable列名 修改DataTable 列的顺序
  8. 如何使用 Gin 和 Gorm 搭建一个简单的 API 服务 (二)
  9. 【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
  10. JS图片的放大与缩小