LeetCode(33)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 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
}
};
LeetCode(33)Search in Rotated Sorted Array的更多相关文章
- LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- 原生JavaScript之实战 模拟重力场(篮球)
成品图如下所示: 点击篮球让篮球掉下 搭建HTML+CSS代码 html: <div id="demo"></div> css: div{ width:10 ...
- BZOJ4571
BZOJ4571 Description Transmission Gate 给定n个数, m次询问, 每次询问[l,r]范围内的数加上x后异或b的最大值, x, b给出. \[n,m <= 2 ...
- _bzoj1500 [NOI2005]维修数列【真·Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...
- [hdu1695] GCD【莫比乌斯反演】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1695 先把题目转化为求一个数在区间[1, b / k],另一个数在区间[1, d / k]时,这两个数互 ...
- jmeter(五)集合点
集合点: 简单来理解一下,虽然我们的“性能测试”理解为“多用户并发测试”,但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点,每到输入用户名和密码登录时,所有的虚拟 ...
- Oracle中的序列
序列是什么? 序列是用来生成唯一.连续的整数的数据库对象.序列通常用来自动生成主键或唯一键的值.序列可以按升序排列,也可以按照降序排列. 其实Oracle中的序列和MySQL中的自增长差不多一个意思. ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第三天(非原创)
文章大纲 一.课程介绍二.简单功能实现三.图片上传功能实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用. ...
- Android SDK镜像更新网速慢的解决问题
通过更换代理解决 Android SDK 在线更新镜像服务器资源:大连东软信息学院镜像服务器地址:http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:IP ...
- git ---查看工作状态和历史提交
1.git查看状态 -git status 2.版权声明 版权声明:新建一个 LICENSE.txt 文件 开源协议:MIT //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...
- Katalon Studio 安装 配置 简单使用
本教程只针对Katalon Studio进行演示操作. 一.下载 Katalon 官网下载地址:https://www.katalon.com/download/ (需要注册账号) 二.解压.配置 直 ...