原题链接

题意:给你一个目标值,或者返回其在数组中的下标位置,或者返回-1(表示不存在,查找失败)。

例如 0 1 2 4 5 6 7 可能成为 4 5 6 7 0 1 2.

思路分析:

  用二分搜索来找到转折点,也就是最小数的位置。对二分搜索要稍作修改,当a[left]<=a[mid],可以肯定a[left...mid]是升序的,那么a[left]是最小的,也可能最小的在a[mid+1...right]中,所以要比较a[left]和min{a[mid+1...right]},同样对于a[left]>a[mid],做相应的处理。

来看代码:

 class Solution {
public:
int findPos(vector<int>& nums,int left,int right){
//if (nums.empty())//多余
// return -1;
if (left > right||left>=nums.size())//后面这个条件不要,一些测试用例会产生下标越界错误
return -;
int mid = (left + right) / ;
if (nums[left] <= nums[mid])
{
int pos = findPos(nums, mid + , right);
if (pos == -)
return left;
else
return nums[left] < nums[pos] ? left : pos;
}
else
{
int pos = findPos(nums, left, mid - );
if (pos == -)
return mid;
else
return nums[mid] < nums[pos] ? mid :pos;
}
}
int bsearch(vector<int>nums, int left, int right, int key)
{
if (left>right)
return -;
int mid = (left + right) / ;
if (nums[mid] == key)
return mid;
if (nums[mid] < key)
return bsearch(nums, mid + , right, key);
else
return bsearch(nums, left, mid - , key);
}
int search(vector<int>& nums, int target)
{
int len = nums.size();
int pos = findPos(nums, , len);
int index;
if ((index=bsearch(nums, , pos - ,target)) != -)
return index;
else return bsearch(nums, pos, len, target); }
};

search in rotated sorted array leetcode的更多相关文章

  1. Search in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  2. Search in Rotated Sorted Array——LeetCode

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

  3. Find Minimum in Rotated Sorted Array leetcode

    原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...

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

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

  5. [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 migh ...

  6. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  7. [LeetCode]题解(python):081 - Search in Rotated Sorted Array II

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...

  8. Leetcode系列-Search in Rotated Sorted Array

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

  9. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

随机推荐

  1. [SQL]SQL优化34条

    我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! () 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到 ...

  2. Android 解决图片大量下载:软引用必须懂4点

    1.对象的强.软.弱和虚引用为了能更加灵活控制对象的生命周期,需要知道对象引用的4中级别,由高到低依次为 :强引用.软引用.弱引用和虚引用 备注: 这四种的区别: ⑴强引用(StrongReferen ...

  3. iOS 获取当前月份的天数(转)、

    转:http://www.cnblogs.com/kingbo/p/3966855.html . 获取当前月份有多少天 NSCalendar *calendar = [NSCalendarcurren ...

  4. 复杂事件处理引擎—Esper入门

    说明: 以下内容,可以参考Esper官方网站<Qucik start & Tutorial >(顺序做了部分调整). PS:因为英语水平有限(大学期间刚过CET4的英语小盲童一枚) ...

  5. ieee文献免费下载办法

    sci-hub是个神奇的存在,但突然有短时间不能用了,搜索很久,找到了:https://www.zhihu.com/question/68333471/answer/276287163 这个网址会实时 ...

  6. yum改成网易的源

    用网易的源会快很多,步骤如下:http://mirrors.163.com/.help/centos.html 1.首先备份/etc/yum.repos.d/CentOS-Base.repo mv / ...

  7. Python字典按值排序的方法

    Python字典按值排序的方法: 法1: (默认升序排序,加  reverse = True 指定为降序排序) # sorted的结果是一个list dic1SortList = sorted( di ...

  8. POJ 3268 Silver Cow Party 最短路径+矩阵转换

    Silver Cow Party Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  9. CF 1095C Powers Of Two(二进制拆分)

    A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-ne ...

  10. ubuntu系统安装mysql二进制压缩包(tar.gz)以及navicat远程连接服务器(linux系统)

    一.ubuntu安装mysql5.6二进制压缩包(tar.gz) 准备 0. 获取 mysql-5.5.15-linux2.6-i686.tar.gz 二进制安装文件 mysql 官网下载页面选择 L ...