题目

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. SQL标量函数-日期函数

    select day(createtime) from life_unite_product     --取时间字段的天值 select month(createtime) from life_uni ...

  2. spring boot 项目发布运行

    1. maven install 发布jar包 2. java -jar webservice.jar 启动jar包

  3. 理解C++中拷贝构造函数

    拷贝构造函数的功能是用一个已有的对象来初始化一个被创建的同样对象,是一种特殊的构造函数,具有一般构造函数的所有特性,当创建一个新对象的时候系统会自动调用它:其形参是本类对象的引用,它的特殊功能是将参数 ...

  4. mariadb+centos7+主从复制

    MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的 ...

  5. 【OCR技术系列一】光学字符识别技术介绍

    注:此篇内容主要是综合整理了光学字符识别 和OCR技术系列之一]字符识别技术总览,详情见文末参考文献 什么是 OCR? OCR(Optical Character Recognition,光学字符识别 ...

  6. _bzoj1059 [ZJOI2007]矩阵游戏【二分图匹配】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1059 保存匈牙利模板. #include <cstdio> #include & ...

  7. java中的位预算

    public class Demo { public static void main(String[] args) { byte num1 = 3; byte num2 = 5; /*位预算 *nu ...

  8. 导出数据库报错 EXP-00002: 写入导出文件时出错 EXP-00000: 导出终止失败

    解决方法: 1.检查磁盘所在空间是否够用. 2.磁盘修复下 排除故障考虑的地方要全面啊.

  9. 设置UITableViewCell 选中时的背景颜色

    自定义Cell如图 一个View上面放了四个Label 分别连线到.m文件中 @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @pro ...

  10. 1268 和为K的组合 Meet in mid二分思路

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1268&judgeId=193772 给出n = 20个数,问其是 ...