题目:

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. [iOS微博项目 - 2.4] - 重新安排app启动步骤

    github: https://github.com/hellovoidworld/HVWWeibo   A.app启动步骤 1.加入了授权步骤之后,最先要判断app内是否已经登陆了账号 2.在程序启 ...

  2. Leetcode237:Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  3. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  4. AP(应付帐管理)

    --更新供应商地点 PROCEDURE update_vendor_site(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return ...

  5. 设置AD用户属性

    设置AD用户属性proxyaddresses的值 $test01 = [adsi]"LDAP://cn=test01,ou=oo,ou=admins,dc=ddv,dc=com" ...

  6. 为DAG预留群集名称对象(CNO)

    在某些环境中,计算机帐户的创建受到限制或计算机帐户是在非默认计算机容器中创建的,则会预留群集名称对象 (CNO),然后通过为其分配权限来设置 CNO.此外,使用运行 Windows Server 20 ...

  7. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  8. org.apache.catalina.mbeans.ServerLifecycleListener

    Tomcat 启动报错: java.lang.ClassNotFoundException: org.apache.catalina.mbeans.ServerLifecycleListener at ...

  9. 一步步学Mybatis-以接口操作的方式编程(2)

    上一章节中,我们已经搭建玩了最简单的能够运行的HelloWorld环境,这一章将把上章中的操作方式改为以接口的方式操作 我们可以发现在上一个章节中 public static void testBas ...

  10. 使用NuGet安装EntityFramework4.2

    1.下载NuGet 有两种方式下载NuGet 第一种:在微软的档案库下载,下载地址为:http://visualstudiogallery.msdn.microsoft.com/27077b70-9d ...