Suppose an array sorted in ascending order 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.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -133. Search in Rotated Sorted Array


class Solution {
public:
int search(vector<int>& nums, int target) {
int l = , r = nums.size()-;
while (l <= r) {
int mid = (l+r) / ;
if (target == nums[mid])
return mid;
// there exists rotation; the middle element is in the left part of the array
if (nums[mid] > nums[r]) {
if (target < nums[mid] && target >= nums[l])
r = mid - ;
else
l = mid + ;
}
// there exists rotation; the middle element is in the right part of the array
else {
if (target > nums[mid] && target <= nums[r])
l = mid + ;
else
r = mid - ;
} }
return -;
}
};

33.[LeetCode] Search in Rotated Sorted Array的更多相关文章

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

  2. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  3. [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路

    33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...

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

  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 II 在旋转有序数组中搜索之二

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

  7. LeetCode——Search in Rotated Sorted Array II

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

  8. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  9. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. 安装Windows7步骤

    我初次装Linux系统的时候,查过一些资料,然后也有网友教过我,这里做一个总结,和大家分享一下(U盘安装).       事实上我们平时电脑开机的时候,是有个启动顺序的,他并非直接进入我们的操作系统, ...

  2. ZooKeeper系列(2)--基于ZooKeeper实现简单的配置中心

    ZooKeeper节点的类型分为以下几类:  1. 持久节点:节点创建后就一直存在,直到有删除操作来主动删除该节点 2. 临时节点:临时节点的生命周期和创建该节点的客户端会话绑定,即如果客户端会话失效 ...

  3. thinkpad T61 Fn功能键的完整说明及有关问题解决

    FN键的作用Fn 键是一个组合键,它自己本身没有用,需要和其他功能键组合起来以迅速改变操作特征,它可以组合的键在键盘上都用蓝色标注,具体作用为: Fn+F2:锁定计算机显示器.要再次打开计算机显示器, ...

  4. Python 基础 模块

    python 中模块和保定 概念 如果将代码分才投入多个py 文件,好处: 同一个变量名也互不影响.   python 模块导入 要使用一个模块,我们必须先导入该模块.python 使用import ...

  5. docker之MySQL主从复制

    MySQL主从复制 主服务器 配置文件目录 mkdir /var/lib/mysql/master/conf.d 数据存储目录 mkdir var/lib/mysql/master/data 配置my ...

  6. MySQL案例03:(MyCAT报错) [ERROR][$_NIOREACTOR-3-RW] caught err: java.lang.OutOfM emoryError: Unable to acquire 131072 bytes of memory, got 0

    上班坐下来没多久,接同事电话说有两台mysql服务器无法访问,其中这两台服务器是mycat服务器+MySQL服务器,具体处理过程如下: 一.错误信息 错误信息01: :: ::, [INFO ][$_ ...

  7. json数组按照日期先后排序

    var allMyApp = [ {"startDate": "2018-07-07 12:30:00",'name':'aa'}, {"startD ...

  8. MySQL慢日志查询实践

    慢日志查询作用 慢日志查询的主要功能就是,记录sql语句中超过设定的时间阈值的查询语句.例如,一条查询sql语句,我们设置的阈值为1s,当这条查询语句的执行时间超过了1s,则将被写入到慢查询配置的日志 ...

  9. [修正] Firemonkey Android 文字斜粗体显示不全的问题

    问题:Firemonkey Android 平台显示斜粗体文字时,文字右方会有显示不全的问题. 修正代码: 请将 FMX.FontGlyphs.Android.pas 复制到自己的工程目录下,再修改如 ...

  10. SDR软件无线电知识要点(一)噪声系数与噪声因子

    SDR软件无线电知识要点(一)噪声系数与噪声因子 信号质量如何评估 Noise Figure (NF) or sensitivity and Error Vector Magnitude (EVM) ...