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. LUA IO库

    I/O库为文件操作提供两种模式. 简单模式(simple model)拥有一个当前输入文件和一个当前输出文件.而且提供针对这些文件相关的操作.全然模式(complete model)使用外部的文件句柄 ...

  2. iOS dyld: Library not loaded 报错解决

    Xcode 用的是10.1 版本打的苹果包在 ios系统10.0 以上可以正常运行 但是系统9.3的手机安装后直接运行就崩溃 后来插上电脑联调 报错 dyld: Library not loaded: ...

  3. svn造成桌面图标显示问号

    (1)在使用svn客户端的时候桌面的所有图标上面都加了一个“?”.而且在桌面上新建的文件夹或文件都会打个问号,下面是笔者搜集的方法:在桌面创建记事本文件,把这句话复制进去for /r . %%a in ...

  4. Excel工作表保护的密码破解与清除...假装自己破解密码系列?

    有一次我女朋友让我帮忙解一个excel表格的保护密码,然后~用了宏 网上下载来的Excel经常会有工作表保护,也就是无法修改,妄图做任何修改的时候你就会看见这句话: 您试图更改的单元格或图表位于受保护 ...

  5. PhpStorm中实现代码自动换行

    方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...

  6. mitmproxy 在windows上的使用

    mitmproxy 是一个中间件代理, 结合python使用 安装  pip install mitmproxy 在windows上没有mitmproxy 所以只要用mitmdump和mitmwdb ...

  7. SQL宽字节注入

    0x00 概述 - 什么是宽字节注入? 宽字节注入就是因为gbk编码方式需要两个ascii码组合来解码,所以形象的叫做宽字节,这个作为了解即可 -宽字节注入的条件 1) 数据库查询设置为GBK编码 2 ...

  8. Python 爬虫 多进程清洗代理

    利用多线程检测代理网站提供的免费代理是否可用 import requests from lxml import etree import time import multiprocessing def ...

  9. Bessel函数的零点计算 MATLAB

    由于MATLAB自己没有附带贝塞尔函数零点,因此使用起来很不方便,特别是在绘制仿真场量时. 下面给出0-9阶的贝塞尔函数零点的计算公式,其中理论上计算零点个数N在50以内时较为精确: function ...

  10. 虚拟机ubuntu磁盘扩容

    1.虚拟机把磁盘大小进行改动 2.sudo apt-get install gparted 3.打开安装好的应用 4.进行分区改动 5.理论删除sda2和sda5重整后边即可,但此时sda2和sda5 ...