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. 题解——洛谷P4095 [HEOI2013]Eden 的新背包问题(背包)

    思路很妙的背包 用了一些前缀和的思想 去掉了一个物品,我们可以从前i-1个和后i+1个推出答案 奇妙的思路 #include <cstdio> #include <algorithm ...

  2. (转)Awesome Knowledge Distillation

    Awesome Knowledge Distillation 2018-07-19 10:38:40  Reference:https://github.com/dkozlov/awesome-kno ...

  3. 【转载】谈谈自己对REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解

    转载自:https://blog.csdn.net/tantexian/article/details/48196453 SOA: 维基百科解释:SOA:面向服务的软件架构(Service Orien ...

  4. markdown一些网站

    1.https://stackedit.io/editor 2.https://github.com/bioinformatist/LncPipeReporter 3.

  5. 项目Alpha冲刺--5/10

    项目Alpha冲刺--5/10 1.团队信息 团队名称:基于云的胜利冲锋队 成员信息 队员学号 队员姓名 个人博客地址 备注 221500201 孙文慈 https://www.cnblogs.com ...

  6. Vue运行报错--not defined

    按F12键进入调试模式,谷歌总是提示Uncaught ReferenceError: ——is not defined这个错误. 原来是因为虽然是传递的值,但是在函数传参的时候也要加引号,加上引号后就 ...

  7. 转一篇 ShaderVariantCollection介绍的比较详细的文章 感谢作者

    http://www.seven-fire.cn/archives/174 Unity3D Shader加载时机和预编译 焱燚(七火)  |    2016年7月6日  |   UnityShader ...

  8. JavaSE习题 第六章 字符串和正则表达式

    Make efforts eveyday 问答题 1.对于字符串 String s1=new String("ok"); String s2=new String("ok ...

  9. <script src="../build/browser.min.js"></script> 是用来里面babel工具把ES6的语法转成ES5

    <!DOCTYPE html> <html> <head> <script src="../build/react.js">< ...

  10. vue-router两种模式的区别

    参考文献:https://blog.csdn.net/lla520/article/details/77894985/ https://segmentfault.com/a/1190000015123 ...