leetcode-algorithms-33 Search in Rotated Sorted Array

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

解法

要找到一个值,其效率为O(logn),就需要使用二分查找,二分查找是要求要有序的.现在先来看nums=[4,5,6,7,0,1,2],target=5这组数字,我们可看成是[4,5,6,7,inf,inf,inf]于是就变成了有序,同时对于nums=[4,5,6,7,0,1,2],target=1,可看成是[-inf,-inf,-inf,-inf,0,1,2]同样变成有序.接下来只要二分查找即可.

class Solution
{
public:
int search(vector<int>& nums, int target)
{
int low = 0;
int high = nums.size();
while (low < high)
{
int mid = (low + high) / 2; double num = (nums[mid] < nums[0]) == (target < nums[0])
? nums[mid]
: (target < nums[0] ? -INFINITY : INFINITY); if (num < target)
low = mid + 1;
else if (num > target)
high = mid;
else
return mid;
}
return -1;
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-33 Search in Rotated Sorted Array的更多相关文章

  1. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  2. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  3. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

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

  4. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  5. leetcode problem 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 migh ...

  6. LeetCode OJ 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 migh ...

  7. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  9. LeetCode:33. Search in Rotated Sorted Array(Medium)

    1. 原题链接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 题目要求 给定一个按升序排列的 ...

  10. Python 解LeetCode:33. Search in Rotated Sorted Array

    题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值: 思路: 第一遍二分遍历,找到数组中最小值的索引: 第二遍分别对最小值左右两边的数组进行二分查找: class Solution(ob ...

随机推荐

  1. SpringBatch是什么?

    https://segmentfault.com/a/1190000016278038 <dependency> <groupId>org.springframework.bo ...

  2. SpringMVC统一转换null值为空字符串的方法 !

    在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个 ...

  3. Facebook ads_Business Manager

    xmind 下载链接

  4. 在Mac OSX下安装Microsoft Calibri字体

    参考: Where can I find default Microsoft fonts Calibri, Cambria? 在Mac OSX下安装Microsoft Calibri字体 1.下载: ...

  5. pyqt笔记2 布局管理

    https://zhuanlan.zhihu.com/p/28559136 绝对布局 相关方法setGeometry().move() 箱式布局 QHBoxLayout和QVBoxLayout是基本的 ...

  6. SD--批量删除订单

    SD--批量删除订单 在sap应用中常常会需要批量删除一些错误录入的单据,为此开发了一个小程序.该程序为了安全,程序做了一下控制 1.限制用户只能删除自己的订单,不能删除别人输入的订单,如果需要修改一 ...

  7. template-web.js 引用变量、函数

    1.关键字   $imports.+变量/函数 {{if $imports.myParseInt(b.health_money)}} <span class="num"> ...

  8. 【BZOJ】1831: [AHOI2008]逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...

  9. ubuntu16系统磁盘空间/dev/vda1占用满的问题

    参考文档: https://www.cnblogs.com/moonandstar08/p/6091507.html (系统磁盘空间/dev/xvda1占满原因分析) https://blog.csd ...

  10. modal结合art-template

    内容div <div id="modal-cont"></div> 模板tpl <script id="modal-tpl" ty ...