题目:

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. 10 个你需要了解的最佳 javascript 开发实践

    原文:Top 10 “Must Follow” JavaScript Best Practices Javascript 的很多扩展的特性是的它变得更加的犀利, 同时也给予程序员机会创建更漂亮并且更让 ...

  2. UVaLive 6694 Toy Boxes (二分+想法)

    题意:给出n个数,把n个数放在三个盒子里,每个盒子里的数绑在一起,要拿出来任何一个数的时候,所承担的重量是整个盒子的总重量,求最小总重量和. 析:感觉吧,就是轻的放的多一些,拿的次数多一些,大的放的少 ...

  3. input标签写CSS时需要注意的几点(先收藏)

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-05) 飞鱼的声纳顶部的搜索框让我头疼了很长时间,原因是总不能获得跨浏览器的统一样式.主要的问题有这么两个:一是inpu ...

  4. iOS开发-用预处理指令代替注释

    有时候我们在调试程序的时候,想不执行某一段代码或者某些方法,我们会用注释,但是这样看上去就会让你的代码显示“很绿”,此时我们可以用一下代码 #if 0 // 你不想执行的代码 #endif 这样就可以 ...

  5. HITAG 2 125kHz RFID IC Read-Write 256 bits

    Features 256 bits EEPROM memory organized in 8 pages of 32 bits each 32 bits unique factory programm ...

  6. How do I place a group of functions or variables in a specific section?

    http://supp.iar.com/Support/?Note=27498 EWARM v5.xx (and newer) The placement of a few functions in ...

  7. 初识ASP.NET---若干常见错误

    近期在学习ASP.NET的相关知识,期间遇到了一些错误,比較常见的错误总结了一下,希望此文能给ASP.NET刚開始学习的人一些帮助.同一时候记录这些错误也方便今后自己查看. 1.  GridView& ...

  8. AnimImageView

    https://github.com/eltld/AnimImageView

  9. NSNotificationCenter通知中心

    概述 NSNotificationCenter通知中心,通常用于一对一或者一对多的消息传递,即当一个地方改变时,要求改变其他的一些地方,例如当网络请求回来了新的数据,需要刷新本地信息和本地内存里面的界 ...

  10. ios中@class和 #import,两种方式的讨论

    转自:http://blog.sina.com.cn/s/blog_a843a8850101b6a7.html 很多刚开始学习iOS开发的同学可能在看别人的代码的时候会发现有部分#import操作写在 ...