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.

相当于将数组的后面一部折叠到数组的前面去了,本质上也是二分法,这里其实也是有规律可循的:首先要得到转折点,也就是其左边右边都比其大的那一点。给一个start以及一个end,如果nums[mid] > nums[start],那么说明从start到mid也一定是递增的,很容的知道pivot一定在mid+1到end之间,那么递归的去查找就可以了。nums[mid] < nums[start]可以同样的去分析,代码如下所示:

  1. class Solution {
  2. public:
  3. int search(vector<int>& nums, int target) {
  4. int pivot = getPivot(nums, , nums.size() - );
  5. int pos = bSearch(nums, , pivot - , target);
  6. if(pos != -)
  7. return pos;
  8. return bSearch(nums, pivot, nums.size() - , target);
  9. }
  10.  
  11. int getPivot(vector<int>&nums, int start, int end){
  12. if(start > end)
  13. return -;
  14. int mid = start + (end - start)/;
  15. if(nums[start] <= nums[mid]){
  16. int pos = getPivot(nums, mid + , end);
  17. if(pos == -) return start;
  18. else
  19. if(nums[pos] < nums[start])
  20. return pos;
  21. else
  22. return start;
  23. }else{
  24. int pos = getPivot(nums, start, mid);
  25. if(pos == -)
  26. return mid;
  27. if(nums[pos] < nums[end])
  28. return pos;
  29. else
  30. return mid;
  31. }
  32. }
  33.  
  34. int bSearch(vector<int>&nums, int start, int end, int target){
  35. int mid;
  36. while(start <= end){
  37. mid = (start+end)/;
  38. if(nums[mid] > target){
  39. end = mid - ;
  40. }else if(nums[mid] < target){
  41. start = mid + ;
  42. }else{
  43. return mid;
  44. }
  45. }
  46. return -; //返回-1,表明在这一部分没有找到,可以在下一部分查找
  47. }
  48. };

LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)的更多相关文章

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

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

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

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

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

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

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

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

  5. [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. 思路 ...

  6. 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 ...

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

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

  8. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  9. Leetcode系列-Search in Rotated Sorted Array

    做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...

  10. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

随机推荐

  1. 简单介绍--TOSCA自动化测试工具

    1.工具源自 TOSCA由公司Tricentis研发.这是一家来自奥地利的软件初创企业,专门帮助企业开发团队进行自动化软件测试.(百科) TOSCA的思想是,不用会编程的测试人员可以直接上手自动化. ...

  2. .NET BETWEEN方法

    Between 值范围比较 可以判断一个值是否落在区间范围值中. public static bool Between<T>(this T me, T lower, T upper) wh ...

  3. python 打印 九九表

    用Python 打印九九表. print 每打印一行默认会带有换行, 在print语句后加上,end = 't' 会变成tab. 排版会好点. def main(): for i in range(1 ...

  4. RabbitMQ的安装及集群搭建方法

    RabbitMQ安装 1 安装erlang 下载地址:http://www.erlang.org/downloads 博主这里采用的是otp_src_19.1.tar.gz (200MB+) [roo ...

  5. 20145322第四周JAVA程序设计基础学习总结

    20145322 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 一.继承共同行为 何谓继承 1 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再 ...

  6. 20145240《网络对抗》Web安全基础实践

    Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 原理:SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语 ...

  7. uboot的FIT功能

    1.FIT介绍 Flattend Image Tree 英文原版介绍: FIT is formally a FDT, which can include images of various types ...

  8. LeetCode——Is Subsequence

    Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...

  9. centos7 firewall 操作

    一.firewall配置 The configuration for firewalld is stored in various XML files in /usr/lib/firewalld/ a ...

  10. 【医学图像】3D Deep Leaky Noisy-or Network 论文阅读(转)

    文章来源:https://blog.csdn.net/u013058162/article/details/80470426 3D Deep Leaky Noisy-or Network 论文阅读 原 ...