题目

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. mysql 循环批量插入

    背景 前几天在MySql上做分页时,看到有博文说使用 limit 0,10 方式分页会有丢数据问题,有人又说不会,于是想自己测试一下.测试时没有数据,便安装了一个MySql,建了张表,在建了个whil ...

  2. java基本数据类型所占字节数

    JAVA基本数据类型所占字节数是多少?(32位系统) byte     1字节                short    2字节                int      4字节      ...

  3. docker数据存储

    docker数据存储 docker提供了三种类型的数据存储 第一种:将数据直接存储在容器中 第二种:将数据映射到外部的本机目录 第三种:将数据映射到专门的数据卷容器

  4. [AHOI2007]密码箱

    Description 在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可 ...

  5. Bryce1010的微机接口课设

    8086CPU知识回顾 8086 CPU 中寄存器总共为 14 个,且均为 16 位 . 即 AX,BX,CX,DX,SP,BP,SI,DI,IP,FLAG,CS,DS,SS,ES 共 14 个. 而 ...

  6. Service官方教程(4)两种Service的生命周期函数

    Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...

  7. OC中protocol、category和继承的关系--转

    开放封闭原则(OCP)就是,“对扩展开放,对更改封闭”.是所有面向对象设计的一个核心宗旨.感兴趣的可以看百度百科的一些解释:http://baike.baidu.com/view/2493421.ht ...

  8. Android开发学习——游戏开发小demo

    public class MainActivity extends Activity { private GameUI gameUI; @Override protected void onCreat ...

  9. mybatis的mapper.xml文件细节

  10. R in action读书笔记(21)第十六章 高级图形进阶(上)

    16.1 R 中的四种图形系统 基础图形函数可自动调用,而grid和lattice函数的调用必须要加载相应的包(如library(lattice)).要调用ggplot2函数需下载并安装该包(inst ...