题目

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.

分析

该题目是在一个旋转过的有序序列中查找关键字。

显然的,不能用一次遍历顺序查找法,考察的关键是二分搜索算法。

对于一个递增序列,在旋转点前后,也会保持递增排序不变。

所以对该题目首先要找到整个序列中的最小元素,也就是旋转点,然后对两边子序列应用二分搜索,找到目标元素的下标。

AC代码

class Solution {
public:
int search(vector<int>& nums, int target) {
if (nums.empty())
return -1; //找到旋转点
int pivot = findPivot(nums , 0 , nums.size()-1);
int pos = binarySearch(nums, 0, pivot - 1, target);
if (pos != -1)
return pos;
else
pos = binarySearch(nums, pivot, nums.size() - 1, target); return pos != -1 ? pos : -1; } //寻找旋转点
int findPivot(vector<int> &nums , const int &lhs , const int &rhs)
{ if (nums.empty() || lhs > rhs)
return -1; int middle = (lhs + rhs) / 2; //如果中间元素大于左侧首位值lhs,则旋转点要么在lhs要么在middle+1 ~ rhs
if (nums[middle] >= nums[lhs])
{
int pivot = findPivot(nums, middle + 1, rhs);
if (pivot == -1)
return lhs;
else if (nums[lhs] < nums[pivot])
return lhs;
else
return pivot;
}//反之,则旋转点要么在middle要么在lhs~middle-1
else{
int pivot = findPivot(nums, lhs, middle-1);
if (pivot == -1)
return middle;
else if (nums[middle] < nums[pivot])
return middle;
else
return pivot;
}//else
} int binarySearch(vector<int> &nums, const int &lhs , const int &rhs ,int target)
{
if (nums.empty() || lhs > rhs)
return -1; int middle = (lhs + rhs) / 2;
if (nums[middle] == target)
return middle;
else if (nums[middle] < target)
{
return binarySearch(nums, middle + 1, rhs, target);
}
else{
return binarySearch(nums, lhs, middle - 1, target);
}//else
}
};

GitHub测试程序源码

LeetCode(33)Search in Rotated Sorted Array的更多相关文章

  1. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

  2. LeetCode(81) Search in Rotated Array II

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

  3. 【LeetCode 33】Search in Rotated Sorted Array

    Search in Rotated Sorted Array 分段有序的数组,二分查找返回下标,没有返回-1 数组有序之后经过 rotated, 比如:6 1 2 3 4 5  or 5 6 7 8 ...

  4. leetcode第32题--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 ...

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

  6. 33. 81. Search in Rotated Sorted Array *HARD*

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

  7. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

随机推荐

  1. Java知识点脑图

    做服务器开发有十几年了,其中大部分用到的都是Java服务器开发,从JDK1.4到现在的JDK1.8,从基本的Java Application到 J2EE(JBOSS,Glassfish),OSGI,到 ...

  2. Visual Studio 2015 个版本下载

    Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...

  3. Kerberos原理和基础小结

    此篇文章仅做Kerberos的基本原理和基本使用做说明,本人对Kerberos了解有限,也是通过大量英文文档中翻译过来, 加上自己对Kerberos的理解所写,本人英文太菜,看文档看的头昏眼花若有写的 ...

  4. Python上下文管理器(Context managers)

    上下文管理器(Context managers) 上下文管理器允许你在有需要的时候,精确地分配和释放资源. 使用上下文管理器最广泛的案例就是with语句了.想象下你有两个需要结对执行的相关操作,然后还 ...

  5. Python函数缓存

    函数缓存 (Function caching) 函数缓存允许我们将一个函数对于给定参数的返回值缓存起来.当一个I/O密集的函数被频繁使用相同的参数调用的时候,函数缓存可以节约时间.在Python 3. ...

  6. Lightoj 1054 - Efficient Pseudo Code

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1054 题目大意: 给出n,m,问n^m的所有因子之和是多少? 解题思路: 补 ...

  7. Maven环境搭建操作记录

    Maven官方网站: http://maven.apache.org/index.html Maven下载地址: http://maven.apache.org/download.cgi Maven历 ...

  8. Android中集成第三方支付

    常见的第三方支付解决方案 支付宝支付 微信支付 银联支付 Ping++统一支付平台(需要继承服务器端和客户端) 短信支付 支付宝的集成流程 相关资料链接: 支付宝支付指引流程:支付指引流程 支付宝An ...

  9. Android开发学习——android与服务器端数据交互

    1.首先搭建服务器端. 使用MyEclipse开发工具 public class MyServlet extends HttpServlet { @Override protected void do ...

  10. 不支持正在使用的 .Net 组帧模式。有关详细信息,请参阅服务器日志--解决方案

    问题在于 NetTcpBinding 服务端和客户端配置不一致. 至少  客户端和服务端:安全性.是否启用可靠会话以及传输方式必须一致 主要是传输方式导致 "不支持正在使用的 .Net 组帧 ...