题目:

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.

说明:

1)已排序数组查找采用二分查找

2)关键找到临界点

实现:

一、我的代码:

     

 class Solution {
public:
int search(int A[], int n, int target) {
if(n==||n==&&A[]!=target) return -;
if(A[]==target) return ;
int i=;
while(A[i-]<A[i]) i++; int pre=binary_search(A,,i,target);
int pos=binary_search(A,i,n-i,target);
return pre==-?pos:pre;
}
private:
int binary_search(int *B,int lo,int len,int goal)
{
int low=lo;
int high=lo+len-;
while(low<=high)
{
int middle=(low+high)/;
if(goal==B[middle])//找到,返回index
return middle;
else if(B[middle]<goal)//在右边
low=middle+;
else//在左边
high=middle-;
}
return -;//没有,返回-1
}
};

二、网上开源代码:

 class Solution {
public:
int search(int A[], int n, int target) {
int first = , last = n-;
while (first <= last)
{
const int mid = (first + last) / ;
if (A[mid] == target)
return mid;
if (A[first] <= A[mid])
{
if (A[first] <= target && target < A[mid])
last = mid-;
else
first = mid + ;
}
else
{
if (A[mid] < target && target <= A[last])
first = mid + ;
else
last = mid-;
}
}
return -;
}
};

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)的更多相关文章

  1. [LeetCode 题解] Search in Rotated Sorted Array

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...

  2. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  3. 【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 ...

  4. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

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

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

  6. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  7. Java for LeetCode 081 Search in Rotated Sorted Array II

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

  8. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  9. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  10. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

随机推荐

  1. 关于iTunes11.1 不能刷自制固件的解决方案

    由于iTunes升级到11.1后, 苹果在程序里限制了不允许刷自制固件, 所以想刷自制固件暂时只能降级iTunes到11.1版本之前, 这里提供iTunes 11.0.5 的下载地址: Windows ...

  2. commondline 之二 执行类

    E:\cn\zno\commandline\Test.class package cn.zno.commandline; import java.lang.management.ManagementF ...

  3. setsockopt的作用列表

    功能描述: 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值 ...

  4. 监控SQL

    http://www.cnblogs.com/downmoon/archive/2009/08/12/1544764.html

  5. JVM的内存管理机制

    在做Java开发的时候常用的JVM内存管理有两种,一种是堆内存,一种是栈内存.堆内存主要用来存储程序在运行时创建或实例化的对象与变量,例如:我们通过new MyClass()创建的类MyClass的对 ...

  6. .net 下的MVCPager

    http://www.cnblogs.com/jiagoushi/archive/2012/12/16/2820835.html http://blog.itpub.net/9914816/views ...

  7. 如何判断js中的数据类型(转)

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  8. XGrid绑定(转)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Fo ...

  9. 关于extjs中动态添加TabPanel的tab项并以iframe显示的整理

    近来的项目中用到了Extjs 的TabPanel,这也是Extjs最基本,最常用的组件了 网上或者书上的例子里大都是把tab项渲染到一个div中, 这对于在每个Tab页里加载一个页面的情况就不适合了 ...

  10. ClassRequestHandler or VendorRequestHandler wIndex must be less than NumIFs

    P1_ro:20000EEA ClassRequestHandler ; CODE XREF: USB__HandleSetup+38j P1_ro:20000EEA LDRB R0, [R4,#4] ...